例如,这个正则表达式
(.*)<FooBar>
将匹配:
abcde<FooBar>
但我如何让它在多行之间匹配呢?
abcde
fghij<FooBar>
例如,这个正则表达式
(.*)<FooBar>
将匹配:
abcde<FooBar>
但我如何让它在多行之间匹配呢?
abcde
fghij<FooBar>
当前回答
在许多正则表达式方言中,/[\S\ S]*<Foobar>/将满足您的需要。源
其他回答
在JavaScript中,你可以使用[^]*来搜索0到无限个字符,包括换行符。
$ (" # find_and_replace ") .click(函数(){ Var text = $("#textarea").val(); search_term = new RegExp("[^]*<Foobar>", "gi");; replace_term = "替换项"; Var new_text = text。替换(search_term replace_term); $ (" # textarea) .val (new_text); }); < script src = " https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js " > < /脚本> </button id="find_and_replace">查找并替换</button> . < br > < textarea ID = " textarea”>中的 fghij< Foobar> textarea > < /
通常在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
试试这个:
((.|\n)*)<FooBar>
它基本上是说“任何字符或换行符”重复0次或多次。
选项1
一种方法是使用s标志(就像接受的答案一样):
/(.*)<FooBar>/s
演示1
选项2
第二种方法是使用m (multiline)标志和以下任何模式:
/([\s\S]*)<FooBar>/m
or
/([\d\D]*)<FooBar>/m
or
/([\w\W]*)<FooBar>/m
演示2
RegEx电路
jex。Im可视化正则表达式:
一般来说,。不匹配换行符,因此try ((.|\n)*)<foobar>。