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.
Update: I got it to work, apparently the problem wasn't with the regular expressions, it's just that the (.) characters were being escaped. Thanks anyways people.
Normally the dot matches any character except newlines.
So if .*
isn't working, set the "dot matches newlines, too" option (or use (?s).*
).
If you're using JavaScript, which doesn't have a "dotall" option, try [sS]*
. This means "match any number of characters that are either whitespace or non-whitespace" - effectively "match any string".
Another option that only works for JavaScript (and is not recognized by any other regex flavor) is [^]*
which also matches any string. But [sS]*
seems to be more widely used, perhaps because it's more portable.
(.*?)
匹配任何内容 - 我已经使用了多年。
Choose & memorize 1 of the following!!! :)
[sS]*
[wW]*
[dD]*
Explanation:
s
: whitespace S
: not whitespace
w
: word W
: not word
d
: digit D
: not digit
(You can exchange the *
for +
if you want 1 or MORE characters [instead of 0 or more]).
BONUS EDIT:
If you want to match everything on a single line, you can use this:
[^n]+
Explanation:
^
: not
n
: linebreak
+
: for 1 character or more
上一篇: 条件语句中运算符的正则表达式验证
下一篇: 匹配任何东西