?
Solved

How to cover multiple matches in one Perl command?

Posted on 2011-10-14
5
Medium Priority
?
173 Views
Last Modified: 2012-05-12
Hi,
How can I change the following code to cover these cases:

CS:something                ### CS with no space
or
CS: something               ### CS with any number of space after colon
or
Comment:something    ### singular comment with no space after colon
or
Comment: something  ### plural comments with any number of space after colon
or
Comments:something   ### plural comments with no space
or
Comments: something  ### plural comments with any number of space after colon


elsif (/^C(S|omment):\s*(.*\n)/) { 

Open in new window



Thanks,

0
Comment
Question by:Tolgar
  • 2
  • 2
5 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 36969379
elsif (/^C(?:S|omment)[:]\s*(.*)/)
0
 
LVL 9

Accepted Solution

by:
parparov earned 2000 total points
ID: 36969498
You don't need to match the newline.
 elsif(/^C(?:S|omments?)\:\s*(.*)/)

Open in new window

#!perl
my @aa = (
	'CS:something',
	'CS: something',
	'Comment:something',
	'Comment: something',
	'Comments:something',
	'Comments: something'
);

for (@aa) {
	print $1 if /^C(?:S|omments?)\:\s*(.*)/;
	print " matched\n";
}

Open in new window

0
 
LVL 31

Expert Comment

by:farzanj
ID: 36970387
I gave the same solution :(
0
 
LVL 9

Expert Comment

by:parparov
ID: 36970405
To be frank, you didn't - you didn't cover the trailing s in comments.
0
 

Author Comment

by:Tolgar
ID: 36970541
oh yes, I mentioned this missing part while assigning the points. But for some reason it did not show up here.

0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question