例如,这个正则表达式

(.*)<FooBar>

将匹配:

abcde<FooBar>

但我如何让它在多行之间匹配呢?

abcde
fghij<FooBar>

当前回答

在notepad++中你可以使用这个

<table (.|\r\n)*</table>

它将匹配从。开始的整个表

rows and columns

你可以让它成为贪婪的,使用下面的方法,这样它就会匹配第一个,第二个等等表,而不是一次全部匹配

<table (.|\r\n)*?</table>

其他回答

我想在Java中匹配一个特定的if块:

   ...
   ...
   if(isTrue){
       doAction();

   }
...
...
}

如果我使用regExp

if \(isTrue(.|\n)*}

它包含方法块的右大括号,所以我使用

if \(!isTrue([^}.]|\n)*}

从通配符匹配中排除结束大括号。

通常在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

如果您正在使用Eclipse搜索,您可以启用“DOTALL”选项来生成'。'匹配任何字符,包括行分隔符:只需在搜索字符串的开头添加“(?s)”。例子:

(?s).*<FooBar>

在基于java的正则表达式中,可以使用[\s\ s]。

我们也可以用

(.*?\n)*?

匹配所有内容,包括换行符,而不是贪心。

这将使新行成为可选的

(.*?|\n)*?