line break on regular expressions
I'm trying to replace everything between 2 tags, but I'm not able to build the right expression.
This is what I did: /<tag>(.*|\n*)</tag>/
I want it to capture any characters including line breaks.
This is an example of what I need to capture:
<tag>
<div class="feed_title">Some Title</div>
<div class="feed_content">Some text</div>
</tag>
Can some of you tell me what I'm开发者_JS百科 doing wrong?
Here is a link to RegExr and a full example of what the content looks like: http://regexr.com?2t4n1
'#.#m'
The m means MULTILINE, it makes the point able to match the newlines=line breaks \n
EDIT:
as it has been corrected by sharp eyes and good brain, it is evidently '#.+#s'
EDIT2:
As Michael Goldshteyn said, this should work
$ch = '<tag>\s+<div class="feed_title">Some Title</div>\s+<div class="feed_content">Some text</div>\s+</tag>'
preg_match('#<tag>(.+?)</tag>#s',$ch,$match)
There is another solution, without s flag, I think:
preg_match('#<tag>((.|\s)+?)</tag>#',$ch,$match)
But it's more complicated
.
EDIT 3:
I think that the presence of \s in $ch is a nonsense. \s is used in a RE, not in strings.
I wrote that because I was thinking that it could be blanks or \t that could be before <tag> and at the beginning of other lines
\t is written with an escape; that's not a reason to write \s also in a string
Perhaps you meant to do this?
# The ... in this example is your text to match
preg_match("#<tag>(.*?)</tag>#s","...",$matches);
Here is a link to an article on XML data extraction using regular expressions using PHP, which has some good examples.
精彩评论