446

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.

Line
  • 1,529
  • 3
  • 18
  • 42
Walker
  • 4,879
  • 3
  • 20
  • 16
  • 51
    `.*` should work. Can you paste your actual code? – Jacob Eggers Jul 15 '11 at 19:06
  • 4
    What language are you coding in? – Ziggy Jul 15 '11 at 19:08
  • 16
    a dot won't match a newline – fy_iceworld Jul 31 '13 at 21:29
  • 4
    It's not working because there is two spaces between "bought" and "sheep". so `I bought sheep` is wrong and `I bought sheep` is correct. –  Oct 01 '20 at 05:19
  • 1
    `(?s:.)` - the [inline modifier group](https://www.regular-expressions.info/modifiers.html) match any char including line break chars. In your case, it would be something like this: `(?s:.*?)`. Taken from the [answer](https://stackoverflow.com/a/33312193/15164646) by [Wiktor Stribiżew](https://stackoverflow.com/users/3832970/wiktor-stribi%c5%bcew). – Dmitriy Zub Dec 27 '21 at 12:33

17 Answers17

435

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.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • 14
    `.*` doesn't match `\n` but it maches a string that contains only `\n` because it matches 0 character. – Toto Aug 28 '13 at 09:45
  • 1
    if using Javascript don't forget to slash the slash when setting a variable to this pattern eg: var pattern = "[\\s\\S]*"; – Astra Bear Aug 24 '16 at 23:41
  • 2
    the `(?s).*` works for me matched everything including new line – Gujarat Santana May 28 '18 at 06:00
  • 1
    There is a PROBLEM with using /[\s\S]*/ . If you use it in your code and then comment out such code that causes a syntax error because the end of the pattern is taken to mean the end of the comment. You then need to remove that pattern from the commented-out code to make it work. But then if you ever un-comment it again it will not work any more, or not work like it used to, and should. – Panu Logic Oct 24 '18 at 17:10
  • This would be the case with any regex ending in an asterisk. Of course it's easy to avoid by either adding something after `*` (as in `/.*(?:)/`) or using the regex constructor (`var foo = new RegExp(".*")`). – Tim Pietzcker Oct 24 '18 at 19:11
  • I pretty much never want to match 0 of something, so I just use [\s\S]+ instead. – Asa Stallard Dec 06 '18 at 05:19
  • In 2018, the [dotall flag](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/dotAll) was added to the specification. Here we are approaching 2020 and Firefox still hasn't implemented it. Give them some [heat](https://bugzilla.mozilla.org/show_bug.cgi?id=1361856). – Lonnie Best Dec 16 '19 at 11:14
  • Also, it looks like JavaScript actually has dotall now https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/dotAll ? So you could also just – bersling Apr 23 '21 at 08:57
  • @Tim Pietzcker good one! ((?s).*) works perfectly!!! Thanks! – Sahelanthropus Jun 29 '22 at 09:54
246

(.*?) matches anything - I've been using it for years.

Adam Arold
  • 29,285
  • 22
  • 112
  • 207
jdixon2614
  • 2,617
  • 1
  • 10
  • 2
  • 84
    but does that match a newline? – ineedahero Oct 20 '16 at 14:45
  • I used this on Sublime Text and it worked perfectly. Thanks! `("title":".*?")` – SCabralO Jul 06 '17 at 14:14
  • 13
    In Javascript, use `[\s\S]*` or `[^]*`. – Jeff Huijsmans Mar 21 '18 at 09:53
  • I don't know but every time I use this expression, I feel guilty, for not making a specific expression for my use case. If say, `w+` isn't enough, I end up using `.+`. Luckily hasn't come back to bite me yet. – Tushar Oct 01 '18 at 12:26
  • Works for me using Python – suffa Jul 05 '19 at 21:19
  • 2
    This doesn't seem to include new lines or trailing full stops. I found this slight alteration hits everything (?s).* – Mitchb Feb 27 '20 at 08:49
  • That matches 0 or more of anything included in the "." – Sam P Feb 22 '21 at 13:28
  • This definitely doesn't work in Python - in fact, it will do the exact opposite. The `?` makes the expression non-greedy, so it will match *nothing*. Thus: `re.search('(.*)', 'abc').group(1)` -> `'abc'` and `re.search('(.*?)', 'abc').group(1)` -> `''`. In Python, the correct syntax is `(?s)(.*)` (or `re.S` / `re.DOTALL` can be passed in the `flags` argument). – ekhumoro Aug 20 '21 at 13:29
130

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

Tyler
  • 1,762
  • 2
  • 15
  • 8
30

Try this:

I bought (.* )?sheep

or even

I bought .*sheep
Mike Mozhaev
  • 2,367
  • 14
  • 13
27

/.*/ 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.
Danny
  • 669
  • 9
  • 20
12

Because . Find a single character, except newline or line terminator.

So, to match anything, You can use like this: (.|\n)*?

Hope it helps!

Sang Huynh
  • 241
  • 3
  • 10
  • Maybe I am a bit ignorant, but to my knowledge the `?` makes the preceding quantifier lazy, causing it to match as few characters as possible (in the case, zero!) – Danny Dec 16 '20 at 17:13
  • Also, this will only work with LF (`\n`) line endings, but not CR (`\r`) or CRLF (`\r\n`) line endings. – Danny Dec 16 '20 at 17:17
  • nope, not in dart it doesn't – MwBakker Jul 06 '23 at 18:17
7

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.

Danny
  • 669
  • 9
  • 20
Cuong Le Ngoc
  • 11,595
  • 2
  • 17
  • 39
7

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

Josh
  • 6,155
  • 2
  • 22
  • 26
3

(.*?) 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]
oryxfea
  • 141
  • 7
  • 2
    The most common way to do this in JS is `[\s\S]` -- i.e. match spaces and non-spaces. – mpen Dec 08 '15 at 23:41
3

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 /.*/.

Danny
  • 669
  • 9
  • 20
Panu Logic
  • 2,193
  • 1
  • 17
  • 21
  • BTW. it seems the code-excerpts above make Stack Overflow render parts of my answer in italics, that was not my intention. – Panu Logic Oct 24 '18 at 17:42
3

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
Danny
  • 669
  • 9
  • 20
Lonnie Best
  • 9,936
  • 10
  • 57
  • 97
3
  1. Regex:

    /I bought.*sheep./
    

    Matches - the whole string till the end of line

    I bought sheep. I bought a sheep. I bought five sheep.

  2. 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 fivesheep.

    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 "

SridharKritha
  • 8,481
  • 2
  • 52
  • 43
0

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

Alex Montoya
  • 4,697
  • 1
  • 30
  • 31
0

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.

  • 1
    `/.*/i` will not match line breaks. Further, `/i` (the ‘ignore case’ flag) is redundant. – Danny Dec 16 '20 at 17:06
0
<?php
$str = "I bought _ sheep";
preg_match("/I bought (.*?) sheep", $str, $match);
print_r($match);
?>

http://sandbox.phpcode.eu/g/b2243.php

genesis
  • 50,477
  • 20
  • 96
  • 125
0

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.

Malcolm
  • 510
  • 3
  • 5
  • 19
-3

I use this: (.|\n)+ works like a charm for me!

  • 3
    **Never** use this pattern (unless you have to in ElasticSearch regex flavor). It causes huge amount of backtrackings steps, and leads to stack overflow issues. Besides, this solution is mentioned [earlier](https://stackoverflow.com/a/56105382/3832970). – Wiktor Stribiżew Sep 06 '21 at 10:14