Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match anything

Tags:

regex

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.

like image 897
Fogmeister Avatar asked Mar 19 '13 09:03

Fogmeister


People also ask

Does regex match anything?

Matching a Single Character Using Regex ' dot character in a regular expression matches a single character without regard to what character it is. The matched character can be an alphabet, a number or, any special character.

How do you match in regex?

The fundamental building blocks of a regex are patterns that match a single character. Most characters, including all letters ( a-z and A-Z ) and digits ( 0-9 ), match itself. For example, the regex x matches substring "x" ; z matches "z" ; and 9 matches "9" .

What is regex anything?

Short for regular expression, a regex is a string of text that lets you create patterns that help match, locate, and manage text.

What does .*?) Mean in regex?

(. *?) matches any character ( . ) any number of times ( * ), as few times as possible to make the regex match ( ? ). You'll get a match on any string, but you'll only capture a blank string because of the question mark.


2 Answers

The regex .* will match anything (including the empty string, as Junuxx points out).

like image 117
RichieHindle Avatar answered Oct 18 '22 00:10

RichieHindle


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

like image 21
Vinnie James Avatar answered Oct 18 '22 00:10

Vinnie James