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
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.
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.
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" .
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.
^((?!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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With