正则表达式的换行符 [英] line break on regular expressions
问题描述
我正在尝试替换2个标签之间的所有内容,但我无法构建正确的表达式。
I'm trying to replace everything between 2 tags, but I'm not able to build the right expression.
这就是我所做的: /< 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 doing wrong?
以下是RegExr的链接以及内容的完整示例: http:// regexr .com?2t4n1
Here is a link to RegExr and a full example of what the content looks like: http://regexr.com?2t4n1
推荐答案
'#.#m'
m 表示MULTILINE,它使得点能够匹配newlines = line休息 \ n
The m means MULTILINE, it makes the point able to match the newlines=line breaks \n
编辑:
因为它已被锐化修正眼睛和好大脑,显然'#。+#s'
as it has been corrected by sharp eyes and good brain, it is evidently '#.+#s'
EDIT2:
正如Michael Goldshteyn所说,这应该有效
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)
还有另一个解决方案,没有 s 标志,我认为:
There is another solution, without s flag, I think:
preg_match('#<tag>((.|\s)+?)</tag>#',$ch,$match)
但它更复杂
。
编辑3 :
我认为$ ch 中 \s 的存在是无稽之谈。 \s 用于RE,而不是字符串。
I think that the presence of \s in $ch is a nonsense. \s is used in a RE, not in strings.
我写的是因为我认为它可能是空白或 \t 可以在< 标记> 之前和其他行的开头
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 是用逃脱写的;这也不是在字符串中写 \s 的理由
\t is written with an escape; that's not a reason to write \s also in a string
这篇关于正则表达式的换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!