Reputation: 77661
I know it seems a bit redundant but I'd like a regex to match anything.
At the moment we are using ^*$
but it doesn't seem to match no matter what the text.
I do a manual check for no text but the test view we use is always validated with a regex. However, sometimes we need it to validate anything using a regex. i.e. it doesn't matter what is in the text field, it can be anything.
I don't actually produce the regex and I'm a complete beginner with them.
Upvotes: 32
Views: 95835
Reputation: 6062
The chosen answer is slightly incorrect, as it wont match line breaks or returns. This regex to match anything is useful if your desired selection includes any line breaks:
[\s\S]+
[\s\S]
matches a character that is either a whitespace character (including line break characters), or a character that is not a whitespace character. Since all characters are either whitespace or non-whitespace, this character class matches any character. the +
matches one or more of the preceding expression
Upvotes: 38
Reputation: 6073
I know this is a bit old post, but we can have different ways like :
Upvotes: 1
Reputation: 16509
^
is the beginning-of-line anchor, so it will be a "zero-width match," meaning it won't match any actual characters (and the first character matched after the ^
will be the first character of the string). Similarly, $
is the end-of-line anchor.
*
is a quantifier. It will not by itself match anything; it only indicates how many times a portion of the pattern can be matched. Specifically, it indicates that the previous "atom" (that is, the previous character or the previous parenthesized sub-pattern) can match any number of times.
To actually match some set of characters, you need to use a character class. As RichieHindle pointed out, the character class you need here is .
, which represents any character except newlines (and it can be made to match newlines as well using the appropriate flag). So .*
represents *
(any number) matches on .
(any character). Similarly, .+
represents +
(at least one) matches on .
(any character).
Upvotes: 9
Reputation: 281825
The regex .*
will match anything (including the empty string, as Junuxx points out).
Upvotes: 42