Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx for a string to NOT contain two different strings

Tags:

regex

Ok you gurus out there who know Regex!

How do you use reg ex to search a string to make sure it doesn't contain either of two different strings.

Example: Say i want to make sure "FileNTile" doesnt contain File or Tile

Thanks

cnorr

like image 512
chris n Avatar asked Jan 28 '10 23:01

chris n


People also ask

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.

How do I not match a character in regex?

There's two ways to say "don't match": character ranges, and zero-width negative lookahead/lookbehind. Also, a correction for you: * , ? and + do not actually match anything. They are repetition operators, and always follow a matching operator.

How do I match a pattern in regex?

2.1 Matching a Single Character 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" .

How do you check if a string matches a regex?

Use the test() method to check if a regular expression matches an entire string, e.g. /^hello$/. test(str) . The caret ^ and dollar sign $ match the beginning and end of the string. The test method returns true if the regex matches the entire string, and false otherwise.


1 Answers

^((?!File|Tile).)*$

This is unlikely to be a good idea though. Almost every programming environment will have a clearer and more efficient approach with string matching. (eg Python: if 'File' not in s and 'Tile' not in s)

Also not all regex implementations have lookahead. eg. it's not reliable in JavaScript. And there may be issues with newlines depending on mode (multiline, dotall flags).

like image 54
bobince Avatar answered Sep 22 '22 10:09

bobince