Match every word except those listed

My problem is that I need to match if the string contains any words other than the list (s) I am listing.

For example, I might have this approved list:

User1
User2

      

Here are two examples of what should and shouldn't.

Must match (because User3 is not approved):

User1
User2
User3

      

Should not be matched (because every string on the approved list):

User1

      

I've tried search assertions, but they don't actually consume letters as they are trying to match, so with a string like "User1\r\nUser2"

I get matches like "ser1\r\n"

. I want to know if there are other words besides what I think are valid.

I cannot use a programming language for this; I am allowed to pass a regular expression to the program. The language will be Perl.

+3


source to share


3 answers


Does /\b((?!(User1\b|User2\b)).+?)\b/

what you are looking for?

\b

means a word break, i.e. a break between a word and a character without a word (zero width).

?!

means negative expression (also zero width).



.+?

used to capture anything that doesn't match the excluded words.

Hope it helps.

+5


source


\b(?!(User1|User2))\w+\b

      



This must match any word not listed in "|" delimited list

+2


source


/\b(?!User[12]\b)\w+/

      

will match any word (character string containing a-zA-Z0-9_

) other than User1

and User2

.

0


source







All Articles