Friday, 22 April 2016

Using different Regex operations



Example of Regex operations
* All examples below are having a pattern, matched strings in green and unmatched in red.


Concatenation
Simplest ; Concatenating a bunch of symbols together
aabaab 
aabaab 
every other string 

Logical OR (Alternation)
To choose from one of several possibilities
aa | baab 
aa
baab 
every other string 

Replication
To specify infinitely many possibilities ; Note : 0 replications of b are permitted.
ab*a 
aa
aba 
abba 
ab
ababa

Grouping
To specify precedence to the various operators 
The replication operator has the highest precedence, then concatenation, then logical OR.
a(a|b)aab 
aaaab
abaab 
every other string

Wildcard
It matches exactly one occurrence of any single character.
a..a 
abba
abaa 
aa
aaaaa 

One or more
a(bc)+de 
abcde
abcbcde 
ade
abc

Once or Not at all
a(bc)?de 
ade
abcde
abc
abcbcde

Character classes (Range)
[a-m]* 
blackmail
imbecile 
above
below

Negation of character classes (Range)
[^aeiou] 
b
c 
a
e

Exactly N times
[^aeiou]{6} 
rhythm
syzygy 
rhythms
allowed

Between M and N times
[a-z]{4,6} 
spider
tiger 
jellyfish
cow

Whitespace characters
[a-z\s]*hello 
hello
say hello 
Othello
2hello

No comments:

Post a Comment

Note: only a member of this blog may post a comment.