例如,这个正则表达式
(.*)<FooBar>
将匹配:
abcde<FooBar>
但我如何让它在多行之间匹配呢?
abcde
fghij<FooBar>
例如,这个正则表达式
(.*)<FooBar>
将匹配:
abcde<FooBar>
但我如何让它在多行之间匹配呢?
abcde
fghij<FooBar>
当前回答
我想在Java中匹配一个特定的if块:
...
...
if(isTrue){
doAction();
}
...
...
}
如果我使用regExp
if \(isTrue(.|\n)*}
它包含方法块的右大括号,所以我使用
if \(!isTrue([^}.]|\n)*}
从通配符匹配中排除结束大括号。
其他回答
这对我来说是最简单的方法:
(\X*)<FooBar>
对于Eclipse,下面的表达式是有效的:
喷火 jadajada酒吧”
正则表达式:
Foo[\S\s]{1,10}.*Bar*
在许多正则表达式方言中,/[\S\ S]*<Foobar>/将满足您的需要。源
通常在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
在语言内部使用的上下文中,正则表达式作用于字符串,而不是行。因此,假设输入字符串有多行,您应该能够正常使用正则表达式。
在这种情况下,给定的正则表达式将匹配整个字符串,因为存在"<FooBar>"。根据regex实现的具体情况,$1值(从"(.*)"中获得)将是"fghij"或"abcde\nfghij"。正如其他人所说,一些实现允许您控制“.”是否匹配换行符,从而让您做出选择。
基于行的正则表达式通常用于命令行,例如egrep。