例如,这个正则表达式
(.*)<FooBar>
将匹配:
abcde<FooBar>
但我如何让它在多行之间匹配呢?
abcde
fghij<FooBar>
例如,这个正则表达式
(.*)<FooBar>
将匹配:
abcde<FooBar>
但我如何让它在多行之间匹配呢?
abcde
fghij<FooBar>
当前回答
问题是,能否。模式匹配任何字符?答案因引擎而异。主要区别在于该模式是由POSIX正则库使用还是由非POSIX正则库使用。
关于lua-pattern需要特别注意:它们不被认为是正则表达式,但是。匹配任何字符,与基于posix的引擎相同。
关于matlab和八度音阶的另一个注意事项:默认匹配任何字符(演示):str = "abcde\n fghij<Foobar>";expression = '(.*)<Foobar>*';[tokens,matches] = regexp(str,expression,'tokens','match');(令牌包含abcde\n fghij项)。
此外,在boost的所有正则表达式语法中,点默认匹配换行符。Boost的ECMAScript语法允许您使用regex_constants::no_mod_m (source)关闭此功能。
对于oracle(它是基于POSIX的),使用n选项(演示):select regexp_substr('abcde' || chr(10) ||' fghij<Foobar>', '(.*)<Foobar>', 1,1, 'n', 1) As results from dual
基于posix的引擎:
一个纯粹的。已经匹配换行符,所以不需要使用任何修饰符,参见bash (demo)。
tcl (demo), postgresql (demo), r (TRE, base r默认引擎不带perl=TRUE,对于base r带perl=TRUE或对于stringr/stringi模式,使用(?s)内联修饰符)(demo)也可以处理。同样的方法。
但是,大多数基于posix的工具都是逐行处理输入的。因此,。不匹配换行符,因为换行符不在范围内。下面是一些如何覆盖它的例子:
sed - There are multiple workarounds. The most precise, but not very safe, is sed 'H;1h;$!d;x; s/\(.*\)><Foobar>/\1/' (H;1h;$!d;x; slurps the file into memory). If whole lines must be included, sed '/start_pattern/,/end_pattern/d' file (removing from start will end with matched lines included) or sed '/start_pattern/,/end_pattern/{{//!d;};}' file (with matching lines excluded) can be considered. perl - perl -0pe 's/(.*)<FooBar>/$1/gs' <<< "$str" (-0 slurps the whole file into memory, -p prints the file after applying the script given by -e). Note that using -000pe will slurp the file and activate 'paragraph mode' where Perl uses consecutive newlines (\n\n) as the record separator. gnu-grep - grep -Poz '(?si)abc\K.*?(?=<Foobar>)' file. Here, z enables file slurping, (?s) enables the DOTALL mode for the . pattern, (?i) enables case insensitive mode, \K omits the text matched so far, *? is a lazy quantifier, (?=<Foobar>) matches the location before <Foobar>. pcregrep - pcregrep -Mi "(?si)abc\K.*?(?=<Foobar>)" file (M enables file slurping here). Note pcregrep is a good solution for macOS grep users.
看演示。
Non-POSIX-based引擎:
php - Use the s modifier PCRE_DOTALL modifier: preg_match('~(.*)<Foobar>~s', $s, $m) (demo) c# - Use RegexOptions.Singleline flag (demo): - var result = Regex.Match(s, @"(.*)<Foobar>", RegexOptions.Singleline).Groups[1].Value;- var result = Regex.Match(s, @"(?s)(.*)<Foobar>").Groups[1].Value; powershell - Use the (?s) inline option: $s = "abcde`nfghij<FooBar>"; $s -match "(?s)(.*)<Foobar>"; $matches[1] perl - Use the s modifier (or (?s) inline version at the start) (demo): /(.*)<FooBar>/s python - Use the re.DOTALL (or re.S) flags or (?s) inline modifier (demo): m = re.search(r"(.*)<FooBar>", s, flags=re.S) (and then if m:, print(m.group(1))) java - Use Pattern.DOTALL modifier (or inline (?s) flag) (demo): Pattern.compile("(.*)<FooBar>", Pattern.DOTALL) kotlin - Use RegexOption.DOT_MATCHES_ALL : "(.*)<FooBar>".toRegex(RegexOption.DOT_MATCHES_ALL) groovy - Use (?s) in-pattern modifier (demo): regex = /(?s)(.*)<FooBar>/ scala - Use (?s) modifier (demo): "(?s)(.*)<Foobar>".r.findAllIn("abcde\n fghij<Foobar>").matchData foreach { m => println(m.group(1)) } javascript - Use [^] or workarounds [\d\D] / [\w\W] / [\s\S] (demo): s.match(/([\s\S]*)<FooBar>/)[1] c++ (std::regex) Use [\s\S] or the JavaScript workarounds (demo): regex rex(R"(([\s\S]*)<FooBar>)"); vba vbscript - Use the same approach as in JavaScript, ([\s\S]*)<Foobar>. (NOTE: The MultiLine property of the RegExp object is sometimes erroneously thought to be the option to allow . match across line breaks, while, in fact, it only changes the ^ and $ behavior to match start/end of lines rather than strings, the same as in JavaScript regex) behavior.) ruby - Use the /m MULTILINE modifier (demo): s[/(.*)<Foobar>/m, 1] rtrebase-r - Base R PCRE regexps - use (?s): regmatches(x, regexec("(?s)(.*)<FooBar>",x, perl=TRUE))[[1]][2] (demo) ricustringrstringi - in stringr/stringi regex funtions that are powered with the ICU regex engine. Also use (?s): stringr::str_match(x, "(?s)(.*)<FooBar>")[,2] (demo) go - Use the inline modifier (?s) at the start (demo): re: = regexp.MustCompile(`(?s)(.*)<FooBar>`) swift - Use dotMatchesLineSeparators or (easier) pass the (?s) inline modifier to the pattern: let rx = "(?s)(.*)<Foobar>" objective-c - The same as Swift. (?s) works the easiest, but here is how the option can be used: NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionDotMatchesLineSeparators error:®exError]; re2, google-apps-script - Use the (?s) modifier (demo): "(?s)(.*)<Foobar>" (in Google Spreadsheets, =REGEXEXTRACT(A2,"(?s)(.*)<Foobar>"))
关于(?s)的说明:
在大多数非posix引擎中,可以使用(?s)内联修饰符(或嵌入式标志选项)来强制执行。匹配换行符。
If placed at the start of the pattern, (?s) changes the bahavior of all . in the pattern. If the (?s) is placed somewhere after the beginning, only those .s will be affected that are located to the right of it unless this is a pattern passed to Python's re. In Python re, regardless of the (?s) location, the whole pattern . is affected. The (?s) effect is stopped using (?-s). A modified group can be used to only affect a specified range of a regex pattern (e.g., Delim1(?s:.*?)\nDelim2.* will make the first .*? match across newlines and the second .* will only match the rest of the line).
POSIX注意:
在非posix正则表达式引擎中,为了匹配任何字符,可以使用[\s\ s] / [\d\ d] / [\w\ w]结构。
在POSIX中,[\s\ s]不匹配任何字符(就像在JavaScript或任何非POSIX引擎中一样),因为括号表达式内不支持正则转义序列。[\s\ s]被解析为匹配单个字符\或s或s的括号表达式。
其他回答
在notepad++中你可以使用这个
<table (.|\r\n)*</table>
它将匹配从。开始的整个表
rows and columns你可以让它成为贪婪的,使用下面的方法,这样它就会匹配第一个,第二个等等表,而不是一次全部匹配
<table (.|\r\n)*?</table>
解决方案:
使用模式修饰符sU将在PHP中获得所需的匹配。
例子:
preg_match('/(.*)/sU', $content, $match);
来源:
模式修饰符
([\ s \ s] *) < FooBar >
点匹配除换行符(\r\n)以外的所有字符。所以使用\s\ s,它将匹配所有字符。
在语言内部使用的上下文中,正则表达式作用于字符串,而不是行。因此,假设输入字符串有多行,您应该能够正常使用正则表达式。
在这种情况下,给定的正则表达式将匹配整个字符串,因为存在"<FooBar>"。根据regex实现的具体情况,$1值(从"(.*)"中获得)将是"fghij"或"abcde\nfghij"。正如其他人所说,一些实现允许您控制“.”是否匹配换行符,从而让您做出选择。
基于行的正则表达式通常用于命令行,例如egrep。
这取决于语言,但应该有一个可以添加到正则表达式模式的修饰符。在PHP中是:
/(.*)<FooBar>/s
结尾的s使点匹配所有字符,包括换行符。