Friday 28 November 2014

some basic and usable regular expression

Standard
automated text manipulation is not easy but if you use regular expression then it will be easy. today i will write some easy and simple regular expression that most usable.



regular expression

'ab*' - a followed by zero or more b
'ab+' - a followed by one or more b
'ab?' - a followed by zero or one b
'ab{3}' - a followed by three b
'ab{2,3}' - a followed by two or three b
'[ab]' - either a or b
'a[ab]+' - a followed by 1 or more a or b
'a[ab]+?' - a followed by 1 or more a or b, not greedy
'[^-. ]+' - sequence without -, . , or space
'[a-z]+' - sequences of lowercase letters
'[A-Z]+' - sequences of uppercase letters
'[A-Z][a-z]+' - one uppercase followed by lowercase
'a.' - a followed by any one character
'a.*b' - a followed by anything, ending in b
'a.*?b' - a followed by anything, ending in b
r'\d+' - sequence of digits
r'\D+' - sequence of non digits
r'\s+' - sequence of space
r'\S+' - sequence of non space
r'\w+' - alphanumeric character
r'\W+' - non alphanumeric character
r'^\w+' - word at start of string
r'^\A\w+' - word at start of string
r'^\w+\S*$' - word near end of string, skip punctuation
r'^\w+\S*\Z' - word near end of string, skip punctuation
r'^\w*t\w*' - word containing t
r'\bt\w+' - t at start of word
r'\w+t\b' - t at end of word
r'\Bt\B' - t, not start or end of word
'a(ab)' - a followed by literal ab
'a(a*b*)' - a followed by 0-n a and 0-n b
'a(ab)*' - a followed by 0-n ab
'a(ab)+' - a followed by 1-n ab
r'^(\w+)' - word at start of string
r'(\w+)\S*$' - word at end, with optional punctuation
r'(\bt\w+)\W+(\w+)' - word starting with t, another word
r'(\w+t)\b' - word ending with t


ok lets try it and enjoy :)