Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all strings except one string using regex [duplicate]

Tags:

regex

I want to match all strings except the string "ABC". Example:

 "A"     --> Match  "F"     --> Match  "AABC"  --> Match  "ABCC"  --> Match  "CBA"   --> Match  "ABC"   --> No match 

I tried with [^ABC], but it ignores "CBA" (and others).

like image 384
PT Huynh Avatar asked Apr 06 '13 15:04

PT Huynh


People also ask

How do you match duplicate words in regex?

Following example shows how to search duplicate words in a regular expression by using p. matcher() method and m. group() method of regex. Matcher class.

How do you exclude words in regex?

To match any character except a list of excluded characters, put the excluded charaters between [^ and ] . The caret ^ must immediately follow the [ or else it stands for just itself.

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.

What does \+ mean in regex?

Example: "a\+" matches "a+" and not a series of one or "a"s. ^ the caret is the anchor for the start of the string, or the negation symbol. Example: "^a" matches "a" at the start of the string. Example: "[^0-9]" matches any non digit.


2 Answers

^(?!ABC$).* 

matches all strings except ABC.

like image 122
Tim Pietzcker Avatar answered Oct 15 '22 19:10

Tim Pietzcker


Judging by you examples, I think you mean "all strings except those containing the word ABC".

Try this:

^(?!.*\bABC\b) 
like image 25
Bohemian Avatar answered Oct 15 '22 21:10

Bohemian