Quantcast
Channel: web – Imran Tariq's Blog
Viewing all articles
Browse latest Browse all 33

Regular Expression caret or circumflex (^) in character class

$
0
0

You can also use a character class to exclude specific characters by adding a caret or circumflex (^) immediately after the opening square bracket like this:
[^aeiou]

This excludes all vowels from a match. So, to[^aeiou] matches top, but not too.

The caret must come first. If it appears anywhere else in the character class, it’s treated as a literal character. For example, [ae^iou] does not mean either a or e, but not i, o, or u. It means any vowel or a caret.

The ^ (circumflex or caret) outside square brackets means look only at the beginning of the target string, for example, ^Win will not find Windows in STRING1 but ^Moz will find Mozilla.

The ^ (circumflex or caret) inside square brackets negates the expression (we will see an alternate use for the circumflex/caret outside square brackets later), for example, [^Ff] means anything except upper or lower case F and [^a-z] means everything except lower case a to z.


Viewing all articles
Browse latest Browse all 33