Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Match pattern

Status
Not open for further replies.

MeganP

Programmer
Sep 23, 2004
18
US
HI,
I am newbie to Perl. I started supporting one application that has one perl program and it needs modification. It does get next record if current record doesn't match pattern. I couldn't understand the use of s* and , in the match pattern. Could some one explain me in detail, please?

next if $_ !~ /GREEN\s*\, \s*ZONE\s*\,/;

Thanks!
 
Megan

\s* means 'any number of whitespace characters' where whitespace includes spaces, tabs etc. So it's looking for lines that contain a string consisting of the following pattern.
Code:
GREEN
any number of spaces, including none
comma
any number of spaces, including none
ZONE
any number of spaces, including none
comma

What do you have to change it to search for now?
 
The \s matches any whitespace character, and the * means zero or more. The [b\,[/b] matches a comma. This should match any string that has GREEN followed by a comma followed by ZONE followed by a comma, with any amount of whitespace (or none).

As an aside, I don't think the backslash is necessary before the comma.
 
Thanks for your prompt reply. Previously GREEN and ZONE could find next to each other (side by side column values), but now there are two empty columns between them. If I could make the following change, Would it be suffiecient?

next if $_ !~ /PECO\s*\,\,\,\s*ZONE\s*\,/;
 
Assuming that the intervening commas in the file are always contiguous, i.e. no intervening whitespace, then yes. As Aardvark has already noted, you don't need to escape the commas. Also, the default target for the regex match is $_ anyway, and unless might be more intuitive in this context, so you could reduce it to
Code:
next unless /PECO\s*,,,\s*ZONE\s*,/;
 
And in case there is whitespace between the commas, you could use
Code:
next unless /PECO\s*,\s*,\s*,\s*ZONE\s*,/;
 
aardvark

Of course, that makes it worthwhile to save 3 characters by doing
Code:
next unless /PECO(\s*,){3}\s*ZONE\s*,/;
At this rate we'll have saved a megabyte by the time we retire...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top