Sayed, Irfan (Irfan) wrote:
>
> From: Rob Dixon [mailto:[EMAIL PROTECTED]
>>
>> Sayed, Irfan (Irfan) wrote:
>>>
>>> Hi All,
>>>
>>> I have a string like this
>>>
>>> CLEARCASE_CMDLINE = (mkact -nc notme sprint) Now with the regular
>>> expression what I want is only those characters before closing braces
>>> excluding white space character. I mean to say that if regular
>>> expression encounter the white space character then it should stop.
>>> So my output should come as sprint.
>>
>> Does the program below do what you need?
>>
>>
>> use strict;
>> use warnings;
>>
>> my $str = 'CLEARCASE_CMDLINE = (mkact -nc notme sprint)';
>>
>> $str =~ /(\S+)\s*\)/ or die "String didn't match expected pattern";
>>
>> my $word = $1;
>> print "Word found is '$word'\n";
>>
>
> Thanks Rob. It really helped. I got what I want.
>
> Request you to please explain me in detail how you did this.
>
> I did not understand much. Please guide.

OK

- \S matches any non-space character, so \S+ matches a sequence of
  one or more of them

- \s matches any space character, so \s* matches an optional
  sequence of them

- \) matches a closing parenthesis

- so the regular expression /\S+\s*\)/ matches a sequence of
  non-space characters, followed optionally by a some whitespace,
  followed by a closing parenthesis, which is what the relevant
  part of your string looks like

- finally, putting parentheses around \S causes the string of
  non-space characters to be stored in $1

I hope that makes it clearer.

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to