Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match opposite of regular expression

Tags:

regex

I have a quick question about Regular Expressions.

Can I specify a pattern and have everything else that doesnt fit the pattern to be matched?

For example, anything that does not fit into this pattern: HT\d{4}, I want to consider a match.

like image 395
Jason Avatar asked May 14 '11 01:05

Jason


People also ask

How do you reverse a regular expression?

You can invert sets by adding a circumflex ^ at the beginning of the set. [^a] matches all except a and [^0-9] all everything else but digits. The circumflex is located on the key 6. In this regular expression, you can invert the set by adding a circumflex behind the first bracket.

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]+/ .

Which is match for regular expression?

The Match(String, String, RegexOptions, TimeSpan) method returns the first substring that matches a regular expression pattern in an input string. For information about the language elements used to build a regular expression pattern, see Regular Expression Language - Quick Reference.

What is '?' In regular expression?

'?' is also a quantifier. Is short for {0,1}. It means "Match zero or one of the group preceding this question mark." It can also be interpreted as the part preceding the question mark is optional. e.g.: pattern = re.compile(r'(\d{2}-)?\


1 Answers

yes, you can do this: (?!HT\d{4})

It's called a "negative lookahead". It is supported in most regex engines.

like image 182
Cheeso Avatar answered Oct 05 '22 09:10

Cheeso