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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Effect of Regex Modifiers

Status
Not open for further replies.

cpic100

Programmer
Joined
Aug 14, 2006
Messages
3
Location
GB
In the line

if ($line =~ m@ <SOME STUFF TO MATCH>.*\s.*<SOME MORE STUFF TO MATCH>.*$@mg)

what does the \s and .*$@mg actualy mean/do?

I think it should search a string for some stuff on one line, then skip to the next line and look for some other stuff, but it doesnt seem to do anything.

 
the \s means a single space

.*$@mg

.* means zero or more of any character and the $ is the end of string anchor so it means zero or more of any characters at the end of the string.

@ is the regexp delimiter in this particular regexp, its being used in place of a forward slash as the delimiters.

'm' means to treat the regexp as multiple lines and the 'g' means to find all occurances of the pattern in the string, which has no use in that particular regexp since it's just an "if" condition. You use the 'g' option to return a list of matches or the number of matches in scalar context.
 
Thanks for that.

So if the m means multiple lines, there is no need to ad anything to the search to ensure that it searches over two lines (i.e the lines are consecutive and seperated by a newline)?
 
you probably want the 's' option instead of the 'm' option for that type of matching.
 
Having changed m to s and replaced the \s with .*, I'm now getting results - but too many of them. Instead of stopping after two line sof the file the script is searching it matches everything to the end of the file.

The file being searched basically consists of questios and answers following a line after one another, so

Question 1
Answer 1
Question 2
Answer 2
Question 3
...

and what I want it to do is find and store each Q & A seperately from each of the others.

I know this is a bit too much of a general description, but any pointers would be great.
 
Not sure if I understand, but if I do then you should not be using a regexp for this purpose. Post some real lines of the file in question for more help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top