例如,这个正则表达式
(.*)<FooBar>
将匹配:
abcde<FooBar>
但我如何让它在多行之间匹配呢?
abcde
fghij<FooBar>
例如,这个正则表达式
(.*)<FooBar>
将匹配:
abcde<FooBar>
但我如何让它在多行之间匹配呢?
abcde
fghij<FooBar>
当前回答
Use:
/(.*)<FooBar>/s
s使点(.)匹配回车符。
其他回答
这取决于语言,但应该有一个可以添加到正则表达式模式的修饰符。在PHP中是:
/(.*)<FooBar>/s
结尾的s使点匹配所有字符,包括换行符。
在notepad++中你可以使用这个
<table (.|\r\n)*</table>
它将匹配从。开始的整个表
rows and columns你可以让它成为贪婪的,使用下面的方法,这样它就会匹配第一个,第二个等等表,而不是一次全部匹配
<table (.|\r\n)*?</table>
我也遇到过同样的问题,我解决的方法可能不是最好的,但确实有效。在我做真正的比赛之前,我替换了所有换行符:
mystring = Regex.Replace(mystring, "\r\n", "")
我在操作HTML,所以在这种情况下换行对我来说并不重要。
我尝试了上面所有的建议,但都没有成功。我使用的是。net 3.5供你参考。
通常在PowerShell中搜索三个连续的行,它看起来像这样:
$file = Get-Content file.txt -raw
$pattern = 'lineone\r\nlinetwo\r\nlinethree\r\n' # "Windows" text
$pattern = 'lineone\nlinetwo\nlinethree\n' # "Unix" text
$pattern = 'lineone\r?\nlinetwo\r?\nlinethree\r?\n' # Both
$file -match $pattern
# output
True
奇怪的是,这将是Unix文本在提示符,但Windows文本在文件中:
$pattern = 'lineone
linetwo
linethree
'
下面是打印行结束符的方法:
'lineone
linetwo
linethree
' -replace "`r",'\r' -replace "`n",'\n'
# Output
lineone\nlinetwo\nlinethree\n
Use:
/(.*)<FooBar>/s
s使点(.)匹配回车符。