Regular expressions, as defined in POSIX 1003.2, come in two forms: extended regular expressions, and basic regular expressions. The basic regular expression is no longer practiced. They may found in some old programs. They will be discussed at the end. So extended regular expression is applied in all the following descriptions.
Let me write a few examples of regular expression:
'^.{25}' # select 25 characters from the beginning
'^[[:blank:]]* # select leading space
'[[:blank:]]*$' # select trailing space
'0x[[:xdigit:]]+' # select hexadecimal number
'[0-9]{1,2}[/.-][0-9]{1,2}[/.-][0-9]{2,4}' # select date seperator may be '/' or '/' or '-'.
If you are learing regular expression very first time, you may not undersdant them correctly as there are certain special characters that are interpreted differently as follows:
Special Characters
------------------
'c' any non-special character c matches itself
'a' matches 'a'
'\c' turn off any special meaning of character c
'\*' matches '*'
'.' matches any single character.
'A.' matches 'Aa', 'A%', 'A*', 'A@', 'A^', 'A)', 'A+', 'A$', 'A~' and so on
'?' the preceding item is optional and will be matched at most once.
'A?abc' matches 'Aabc' or 'abc'
'#?1234' matches '#1234' or '1234'
'*' the preceding item will be matched zero or more times.
'Ab*C' matches 'AC', 'AbC', 'AbbC', 'AbbbC', 'AbbbbC', 'AbbbbbC' and so on.
'+' the preceding item will be matched one or more times.
'Ab+C' matches 'AbC', 'AbbC', 'AbbbC', 'AbbbbC', 'AbbbbbC' and so on.
'[...]' any one of character in list enclosed in '[]'.
'c[AB]' matches 'cA' or 'cB' but not 'cD' or 'cE'
'[^...]'any single character not in list enclosed in '[]'.
'c[^AB]' matches 'cD' or 'cE' but not 'cA' or 'cB'
'^' beginning of line
'^#define' matches '#define' that must at the beginning of the line
'$' end of line
'Var;$' matches 'Var;' there must be no character after ';' in the line.
'\d' matches the same sequence of characters matched by the dth parenthesized subexpression
'A(helps)B\1' matches 'AhelpsBhelps'
'|' seperates braches i.e combines two REs and matches any one of them.
'r1|r2' matches r1 or r2.
'{N}' the preceding item is matched exactly N times.
'Ab{4}' matches 'AbbbbC'.
'{N,}' the preceding item is matched n or more times.
'b{4,}' matches 'AbbbbC', 'AbbbbbC', 'AbbbbbbC' and so on.
'{N,M}' the preceding item is matched at least N times, but not more than
M times. N must not exceed M.
'b{3,5}' matches 'AbbbC', 'AbbbbC', 'AbbbbbC'
A Bracket Expression or List
----------------------------
It is a list of characters enclosed in '[]' like:
'[-+*\%]', '[^|$]', '[0-9]','[^a-z]', '[]xyz]', '[]^xyz]', '[-xyz]', or '[xyz-]'.
It normally matches any single character from the list.
'16[-+*\%]4' matches: '16-4', '16+4','16*4','16\4', or '16%4'.
If the list begins with ‘^’, it matches any single character not from the rest of the list.
'b[^c]' matches: ba or bd but not bc
If two characters in the list are separated by ‘-’, this specifies the full range of characters between those two (inclusive).
'[A-Z]cc' matches: Acc, Bcc, Ccc, and so on.
Most of the special characters lose their special significance within a bracket expression.
'c[*/]' matches 'c*' or 'c\'
The best way to learn regular expression is to play with them, so start by creating a file having following lines. Note that numbering is not the part of file contents. I show them just for explaination purpose:
1. A01$1-- xyz
2. #Ab02$1.25--(xyz)
3. #Abb02$1.25--
4. #Bbb03$25.01--@xyz%
5. Cbbb04$353.11--
6. #Cbbbbb05$445.24--0xaaff22dd
7. #Dbbbbcc06$9444--0xyz
8. #Eccbbbb07$56755--
The following table shows some regular expressions with their interpretation and matches:
'.C' :any character followd by 'C' :6
'#?C' :'C' may or may not be preceded by '#' :5,6
'Ab*0' :'A' followed by 'b' zero or more times then followd by '0' :1,2,3
'.c+0' :any character followed by 'c' one or more times then followed by '0' :7
'.b{5}' :any character followed by 'b' exactly five times:6
'.b{2,}' :any character followed by 'b' equal to or more than 2 times: 3,4,5,6,7,8
'.b{3,5}' :any character followed by 'b' between 3 to 5 times inclusive:5,6,7,8
'c[0-9]' :'c' followed by any digit character:7
'b0[^254]' :'b' followed by '0' then followed by any character except '2','5', and '4':4,7,8
'$[0-9]{5}' :'$' followed by five digit characters:8
'cc[[:digit:]]':'cc' followd by a digit character : 7
'cc[[:lower:]]':'cc' followd by a lower case alphabatic character:8
'[[:upper:]]cc': a upper case alphabatic character followed by 'cc':8
'cc[[:alpha:]]': 'cc' followed by any case alphatic character: 8
'cc[[:alnum:]]': 'cc' followed by alphabatic or digit character: 7,8
'[[:space:]]xyz': 'xyz' preceded by at least a single space:1
'[[:punct:]]xyz[[:punct:]]': any punctuation character followed by 'xyz' then followed by punctuation character:2,4
'^[[:graph:]]+xyz':'xyz' preceded by any printable character except blanks one or more times:2,4,7
'^[[:print:]]+xyz':'xyz' preceded by any printable character includeing blanks one or more times:1,2,4,7
'[[:blank:]]xyz':'xyz' preceded by space or tab character: 1
'0x[[:xdigit:]]+':'0x' followed by hexadecimal digit character one or more times:6
grep 'A.[0-9]' text
-- character 'A' followed by any digit
output:
A01$1-- xyz
#Ab02$1.25--(xyz)
'Cb{3}'
-- character 'C' followed by exactly 3 occurrences of character 'b'
output:
Cbbb04$353.11--
'^C'
-- character C at the beginning of the line
Classes

==================================EXAMPLES====================================
Example:A date may appear in any one of the following formats:
01-02-2008 or 01-02-08 or 01-2-2008 or 01-2-08 or 1-02-2008 or 1-02-08
1-2-2008 or 1-2-08
Write regular expression for:
a. date '2-4-2008',
b. date between 1 and 9, month 4 and year 2008.
c. date between 1 and 19, month 4 and year 2008.
Answers
a. Select day that may be either '2' or '02' by RE '0?2'
Similary select month by RE '0?4'
Select the year that may be either '08' or '2008' by RE '(20)?08'
Finally the whole regular expression together is:
'0?2-0?4-(20)?08'
2. '^[0]?[1-9]-[0]?4-[2]?[0]?08'
3. '^([0]?[1-9]|[1][1-9])-[0]?4-[2]?[0]?08'
4. '^[0]?3[-/][0]?4[-/][2]?[0]?08'
'^[0]?[1-9][-/][0]?4[-/][2]?[0]?08'
d. date field seperator is either dash '-', or slash '/', then how will you write above three expressions ?
'^([0]?[1-9]|[1][1-9])[-/][0]?4[-/][2]?[0]?08'
It's supposed date starts from the beginning of a line, if date doesn't start from the beginning, it is necessary to specify where from the date starts. Otherwise it selects all dates. The reason is leaving first digit it matches the pattern for example in date 21-04-2008 it matches 1-04-2008 leaving 2.
Example: A date seperater character may be any of the followings: '-', '/', and '.'. There are two places where this character occurs. How can we force the second character should be same as the first one. That means if first seperator is '.' second would be '.' neither '/' and nor '-' for example:
25-08-2008 correct
25/08/2008 correct
25.08-2008 wrong
25/08-2008 wrong
If I write regular expression for above date representation the expression may be long and it may take much time. So let assume the day as dd, month as mm and year as yyyy
If write regular expression as follows would it be correct ?
'dd[-+/]mm[-+/]yyyy'
No it wouldn't be correct. Becasue it would also select such text as 'dd-mm/yyyy' that is incorrrect. So right regular expression is:
'dd([-+/])mm\1yyyy' using back referencing
No comments:
Post a Comment