Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to *not* match any characters

Tags:

regex

I know it is quite some weird goal here but for a quick and dirty fix for one of our system we do need to not filter any input and let the corruption go into the system.

My current regex for this is "\^.*"

The problem with that is that it does not match characters as planned ... but for one match it does work. The string that make it not work is ^@jj (basically anything that has ^ ... ).

What would be the best way to not match any characters now ? I was thinking of removing the \  but only doing this will transform the "not" into a "start with" ...

like image 983
Erick Avatar asked May 28 '10 15:05

Erick


People also ask

How do I not match a character in regex?

To replace or remove characters that don't match a regex, call the replace() method on the string passing it a regular expression that uses the caret ^ symbol, e.g. /[^a-z]+/ .

How can you negate characters in a set?

Negated Character Classes If you don't want a negated character class to match line breaks, you need to include the line break characters in the class. [^0-9\r\n] matches any character that is not a digit or a line break.

How do I match a character in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).

Is there a regex that will never match anything?

The ^ character doesn't mean "not" except inside a character class ( [] ). If you want to not match anything, you could use a negative lookahead that matches anything: (?!.*). A simple and cheap regex that will never match anything is to match against something that is simply unmatchable, for example: \b\B.

How do you match a character in a regular expression?

'.' character will match any character without regard to what character it is. The matched character can be an alphabet, number of any special character. By default, period/dot character only matches a single character. To create more meaningful patterns, we can combine it with other regular expression constructs.

How do you use regex in regex?

Regex can be used to select everything between the specified characters. This can be useful for things like extracting contents of parentheses like (abc) or for extracting folder names from a file path (e.g. C:/documents/work/). A regular expression that matches all characters between two specified characters makes use of look-ahead (?=…)

How to match everything except a specific character in a string?

To match everything except a specific character, simply insert the character inside the negative lookahead. This expression will ignore any string containing an a: /^(?!.*a).*/


2 Answers

The ^ character doesn't mean "not" except inside a character class ([]). If you want to not match anything, you could use a negative lookahead that matches anything: (?!.*).

like image 66
JSBձոգչ Avatar answered Sep 22 '22 19:09

JSBձոգչ


A simple and cheap regex that will never match anything is to match against something that is simply unmatchable, for example: \b\B.

It's simply impossible for this regex to match, since it's a contradiction.

References

  • regular-expressions.info\Word Boundaries
    • \B is the negated version of \b. \B matches at every position where \b does not.
like image 40
polygenelubricants Avatar answered Sep 24 '22 19:09

polygenelubricants