正则表达式 - 匹配任何东西 [英] Regular Expressions- Match Anything
问题描述
如何使表达式完全匹配任何内容(包括空格)?
示例:
How do I make an expression to match absolutely anything (including whitespaces)?
Example:
正则表达式:我买了 _____ 只羊.
Regex: I bought _____ sheep.
火柴:我买了羊.我买了一只羊.我买了五只羊.
Matches: I bought sheep. I bought a sheep. I bought five sheep.
我尝试使用 (.*)
,但这似乎不起作用.
I tried using (.*)
, but that doesn't seem to be working.
推荐答案
通常点匹配任何字符换行符.
因此,如果 .*
不起作用,请设置点匹配换行符"选项(或使用 (?s).*
).
So if .*
isn't working, set the "dot matches newlines, too" option (or use (?s).*
).
如果您使用的 JavaScript 没有dotall"选项,请尝试 [\s\S]*
.这意味着匹配任意数量的空白或非空白字符"——实际上是匹配任何字符串".
If you're using JavaScript, which doesn't have a "dotall" option, try [\s\S]*
. This means "match any number of characters that are either whitespace or non-whitespace" - effectively "match any string".
另一个仅适用于 JavaScript(并且不被任何其他正则表达式识别)的选项是 [^]*
,它也匹配任何字符串.但是 [\s\S]*
似乎使用更广泛,也许是因为它更便携.
Another option that only works for JavaScript (and is not recognized by any other regex flavor) is [^]*
which also matches any string. But [\s\S]*
seems to be more widely used, perhaps because it's more portable.
这篇关于正则表达式 - 匹配任何东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!