DelphiFAQ Home Search:

Regular expressions over multiple lines


comments2 comments. Current rating: 1 stars (1 votes). Leave comments and/ or rate it.


Can a regular expression span over multiple lines?


No. You have to do tricks like replacing all \n with a certain string (e.g. __NEWLINE__), then use this certain string in your regex and finally replace all __NEWLINE__ with \n again.


2006-09-26, 19:17:21 from United States  
This is incorrect. If you add the 's' switch to the end of the line then you can search across newlines, and ^,$ will match only at the very beginning and end of the string.

For example:

$contents = '<table>
$contents =~ /(<record>.+<\/record>)(.*)(\1)/si;
print 'Result: $1, $2, $3';

2007-12-19, 07:41:39 from Canada  
You can also include \n as a possible match in the regex.

Example (from HTML):

Result: will match all the text between the <ul> tags, even if it spans multiple lines.

Hopefully, you can expand this to other applications.



NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: