| |||||||||||
| |||||||||||
RegEx Posted by matt (matt), 14 March 2003 I am looking for some help with a regular expression. I need to print the line below the one that I have matched. Any ideas?Matt Posted by admin (Graham Ellis), 15 March 2003 The answer to this one rather depends on how the data that you're checking is held or being read. If you're reading data line by line from the file (or other data stream), then set a variable to note where a match occurs or not to any particular read, and print out the current line if that variable holds a true value once you've read in the next line: Code:
If you have "slurped" the whole stream of incoming data, you can do it with a counter on a for loop: Code:
And if you have the whole of the incoming string in a single variable you could do it with a regular expression match: Code:
This last one calls for a bit more explanation as it's likely to look pretty odd to many readers. The whole of your file of incoming data is placed into a scalar, which is then matched against a regular expression using the "g" modifier. This means that each time you run the match statement, it resumes in the string at the point at which the match ended, and it returns a false value once there are no more matches - it's really a match with a memory, where match number n depends on where match n-1 left off. The example uses the ^ operator to indicate "starting with", and the "m" modifier is used to change this so that it will match not only at the very beginning of the string (the default) but also at any embedded new line start. It also uses the fact that the . character (full stop) matches any character EXCEPT a new line since we have NOT specified the "s" modifier. Finally, the round brackets around a part of the match indicate that the part selected is an "interesting bit" (a.k.a. a capture string), and whatever matches the bracket is to be stored into $1 if the match succeeds. Typical Perl - three paragraphs or so to explain a couple of lines of code. I thrive on it! A question for you to ask yourself - what do you want to do / print out if you get a match on the very last line of your data? My examples above simply won't come up with anything to indicate that the final line matched, but you might want to do something different. All three examples have been tested on a file of web log data that I use for purposes such as this, and have correctly printed out 4 lines from a file of just under 7000 lines - I have been told which hosts came along to the web server immediately after "seaweed": Code:
This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |