Monday, March 10, 2014

regex - Character Classes or Character Sets and alternatives(this or that)

Good day to all...
Lets learn two basic and important RegEx..

Character Classes  or Character Sets -  [ ]
As seen on regular-expressions dot info/charclass.html:

With a "character class", also called "character set", you can tell the regex engine to match only one out of several characters. Simply place the characters you want to match between square brackets. If you want to match an a or an e, use [ae]. You could use this in gr[ae]y to match either gray or grey. Very useful if you do not know whether the document you are searching through is written in American or British English.

[aA]pple -- will match for apple or Apple
[^apple]  -- will match for anything other than apple
[0-9] -- will match for 0 to 9
[0-9][A-F] -- will match for a number and a char from A to F
[0-9A-F] -- is equal to [0-9][A-F]
[0-9A-Fa-f] -- if u want to include lowercase a to f as well.

Alternatives -- this or that - |
if you want to match this or that, you can use this|that

That's either 'this' or 'that'. But what if we wanted either 'yes' or 'yet'? To get alternatives on
part of an expression, we need to group the options. In a regular expression, grouping is always
done with parentheses:
To match yes or yet, you can use - ye(t|s)

Please let me know how its going on... unless you say it, i cant understand your mind ;)
thanks, happy coding. happy regex!.

No comments:

Post a Comment