How do I match any character across multiple lines

2018-12-31 00:14发布

For example, this regex

(.*)<FooBar>

will match:

abcde<FooBar>

But how do I get it to match across multiple lines?

abcde
fghij<FooBar>

20条回答
泪湿衣
2楼-- · 2018-12-31 00:32

In JavaScript, use /[\S\s]*<Foobar>/. Source

查看更多
零度萤火
3楼-- · 2018-12-31 00:33

Try this:

((.|\n)*)<FooBar>

It basically says "any character or a newline" repeated zero or more times.

查看更多
妖精总统
4楼-- · 2018-12-31 00:37

generally . doesn't match newlines, so try ((.|\n)*)<foobar>

查看更多
一个人的天荒地老
5楼-- · 2018-12-31 00:38
/(.*)<FooBar>/s

the s causes Dot (.) to match carriage returns

查看更多
不流泪的眼
6楼-- · 2018-12-31 00:39

Note that (.|\n)* can be less efficient than (for example) [\s\S]* (if your language's regexes support such escapes) and than finding how to specify the modifier that makes . also match newlines. Or you can go with POSIXy alternatives like [[:space:][:^space:]]*.

查看更多
浮光初槿花落
7楼-- · 2018-12-31 00:41

Solution:

Use pattern modifier sU will get the desired matching in PHP.

example:

preg_match('/(.*)/sU',$content,$match);

Source:

http://dreamluverz.com/developers-tools/regex-match-all-including-new-line http://php.net/manual/en/reference.pcre.pattern.modifiers.php

查看更多
登录 后发表回答