Reputation: 5189
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.
Upvotes: 476
Views: 1197662
Reputation: 2159
/^/
I can't see why this wouldn't be the best and indeed the simplest and most intuitive option.
Explanation: match any input that has a beginning... Every input always has a beginning and this is the only condition.
Performance: What can be faster than confirming that processing started? It should not require ANY processing -- I can't see how even one character will be looked at. Just a trivial "YES" triggered by the fact that you're asking, nothing more.
Try it yourself: Simplest Regex to Match Any Input (on regexpal.com)
Upvotes: 0
Reputation: 576
One option is the empty regular expression, denoted in JavaScript as /(?:)/
. (You can also use new RegExp()
). Logically, an empty regular expression should match strings containing "emptiness" at any position--which of course is all of them.
See this SO question and this blog post for discussion and more details.
Upvotes: 1
Reputation: 728
/.*/
works great if there are no line breaks. If it has to match line breaks, here are some solutions:
Solution | Description |
---|---|
/.*/s |
/s (dot all flag) makes . (wildcard character) match anything, including line breaks. Throw in an * (asterisk), and it will match everything. Read more. |
/[\s\S]*/ |
\s (whitespace metacharacter) will match any whitespace character (space; tab; line break; ...), and \S (opposite of \s ) will match anything that is not a whitespace character. * (asterisk) will match all occurrences of the character set (Encapsulated by [] ). Read more. |
Upvotes: 31
Reputation: 2263
For JavaScript the best and simplest answer would seem to be /.\*/
.
As suggested by others /(.*?)/
would work as well but /.\*/
is simpler. The ()
inside the pattern are not needed, as far as I can see nor the ending ?
to match absolutely anything (including empty strings)
NON-SOLUTIONS:
/[\s\S]/
does NOT match empty strings so it's not the solution.
/[\s\S]\*/
DOES match also empty strings. But it has a problem: If you use it in your code then you can't comment out such code because the */
is interpreted as end-of-comment.
/([\s\S]\*)/
works and does not have the comment-problem. But it is longer and more complicated to understand than /.*/
.
Upvotes: 3
Reputation: 11975
If you're using JavaScript, ES2018 added the /s
(dotAll) flag. With the /s
flag, the dot .
will match any character, including a newline.
console.log("line_1\nline_2".match(/.+/s))
Note: It's not supported by all browsers yet.
Upvotes: 8
Reputation: 9611
Regex:
/I bought.*sheep./
Matches - the whole string till the end of line
I bought sheep. I bought a sheep. I bought five sheep.
Regex:
/I bought(.*)sheep./
Matches - the whole string and also capture the sub string within () for further use
I bought sheep. I bought a sheep. I bought five sheep.
I boughtsheep. I bought a sheep. I bought five
sheep.
Example using Javascript/Regex
'I bought sheep. I bought a sheep. I bought five sheep.'.match(/I bought(.*)sheep./)[0];
Output:
"I bought sheep. I bought a sheep. I bought five sheep."
'I bought sheep. I bought a sheep. I bought five sheep.'.match(/I bought(.*)sheep./)[1];
Output:
" sheep. I bought a sheep. I bought five "
Upvotes: 4
Reputation: 11354
The 2018 specification provides the s flag (alias: dotAll), so that .
will match any character, including linebreaks:
const regExAll = /.*/s; //notice the 's'
let str = `
Everything
in this
string
will
be
matched. Including whitespace (even Linebreaks).
`;
console.log(`Match:`, regExAll.test(str)); //true
console.log(`Index Location:`, str.search(regExAll));
let newStr = str.replace(regExAll,"🐔");
console.log(`Replaced with:`,newStr); //Index: 0
Upvotes: 3
Reputation: 181
Honestly alot of the answers are old so i found that if you simply just test any string regardless of character content with "/.*/i" will sufficiently get EVERYTHING.
Upvotes: 0
Reputation: 5099
I recommend use /(?=.*...)/g
Example
const text1 = 'I am using regex';
/(?=.*regex)/g.test(text1) // true
const text2 = 'regex is awesome';
/(?=.*regex)/g.test(text2) // true
const text3 = 'regex is util';
/(?=.*util)(?=.*regex)/g.test(text3) // true
const text4 = 'util is necessary';
/(?=.*util)(?=.*regex)/g.test(text4) // false because need regex in text
Use regex101 to test
Upvotes: 1
Reputation: 241
Because .
Find a single character, except newline or line terminator.
So, to match anything, You can use like this: (.|\n)*?
Hope it helps!
Upvotes: 12
Reputation: 1810
Choose & memorize 1 of the following!!! :)
[\s\S]*
[\w\W]*
[\d\D]*
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
Upvotes: 131
Reputation: 141
(.*?)
does not work for me. I am trying to match comments surrounded by /* */
, which may contain multiple lines.
Try this:
([a]|[^a])
This regex matches a
or anything else expect a
. Absolutely, it means matching everything.
BTW, in my situation, /\*([a]|[^a])*/
matches C style comments.
Thank @mpen for a more concise way.
[\s\S]
Upvotes: 3
Reputation: 336108
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 [\s\S]*
. 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 [\s\S]*
seems to be more widely used, perhaps because it's more portable.
Upvotes: 462
Reputation: 50966
<?php
$str = "I bought _ sheep";
preg_match("/I bought (.*?) sheep", $str, $match);
print_r($match);
?>
http://sandbox.phpcode.eu/g/b2243.php
Upvotes: 0
Reputation: 6473
Use .*
, and make sure you are using your implementations' equivalent of single-line so you will match on line endings.
There is a great explanation here -> http://www.regular-expressions.info/dot.html
Upvotes: 7