03 Regular Expressions
03 Regular Expressions
26 July, 2011
Saurabh Barjatiya
IIIT Hyderabad
Basics Miscellaneous
Contents
Basics Matching single character Using special characters Repetition match Miscellaneous OR of regular expressions Backreference
Saurabh Barjatiya
IIIT Hyderabad
Basics Miscellaneous
Sample text le
For all future examples following will be contents of sample text le (say test.txt) Contents ABCD XYZ xyz abcd 340 6$ there are two elfs in this room there are two shelves in this room 341 + 24 = 356 !@#$%
Saurabh Barjatiya Regular Expressions Scripting and Computer Environment - Lecture 3 IIIT Hyderabad
Basics Miscellaneous
All alphabets and numbers match themselves. Hence nothing fancy is required to match simple words or numbers. Example 1 - Matching alphabets and numbers Command: grep this test.txt Output: there are two elfs in this room there are two shelves in this room
Saurabh Barjatiya
IIIT Hyderabad
Basics Miscellaneous
Saurabh Barjatiya
IIIT Hyderabad
Basics Miscellaneous
Special characters can be escaped using backslash (\) so that they match themselves and do not have any special signicance. Example 2 - Matching special characters Command: grep \$ test.txt Output: 6$ !@#$%
Saurabh Barjatiya
IIIT Hyderabad
Basics Miscellaneous
Square brackets ([]) are used to specify range / set of characters that can be matched. Example 3 - Matching set of characters Command: grep [iou] test.txt Output: abcd there are two elfs in this room there are two shelves in this room
Saurabh Barjatiya
IIIT Hyderabad
Basics Miscellaneous
Example 4 - Matching range of characters Command: grep [0-9] test.txt Output: 340 6$ 341 + 24 = 356
Saurabh Barjatiya
IIIT Hyderabad
Basics Miscellaneous
Basics Miscellaneous
Basics Miscellaneous
Basics Miscellaneous
Period (.) Matches any single character Caret (^) Matches empty character at start of line Dollar ($) Matches empty character at end of line (\<) Matches empty character at beginning of word (\>) Matches empty character at end of word (\b) Matches empty character at edge of word
Saurabh Barjatiya
IIIT Hyderabad
Basics Miscellaneous
Saurabh Barjatiya
IIIT Hyderabad
Basics Miscellaneous
Saurabh Barjatiya
IIIT Hyderabad
Basics Miscellaneous
Saurabh Barjatiya
IIIT Hyderabad
Basics Miscellaneous
Example 09 - Usage of \< Command: grep \<s test.txt Output: there are two elfs in this room there are two shelves in this room
Saurabh Barjatiya
IIIT Hyderabad
Basics Miscellaneous
Example 10 - Usage of \> Command: grep s\> test.txt Output: there are two elfs in this room there are two shelves in this room
Saurabh Barjatiya
IIIT Hyderabad
Basics Miscellaneous
Example 11 - Usage of \b Command: grep \b3 test.txt Output: 340 341 + 24 = 356
Saurabh Barjatiya
IIIT Hyderabad
Basics Miscellaneous
? The preceding item is optional and matched at most once. * The preceding item will be matched zero or more times. + The preceding item will be matched one or more times. {n} The preceding item is matched exactly n times. {n,} The preceding item is matched n or more times. {n,m} The preceding item is matched at least n times, but not more than m times.
Saurabh Barjatiya
IIIT Hyderabad
Basics Miscellaneous
Example 12 - Usage of ? Command: grep -o \<t...\?\> test.txt Output: two this two this
Saurabh Barjatiya
IIIT Hyderabad
Basics Miscellaneous
Saurabh Barjatiya
IIIT Hyderabad
Basics Miscellaneous
Example 14 - Usage of + Command: grep -o th[a-z]\+ test.txt Output: there this there this
Saurabh Barjatiya
IIIT Hyderabad
Basics Miscellaneous
Example 15 - Usage of {} Commands: grep -o ro\{2\}m test.txt grep -o ro\{1,\}m test.txt grep -o ro\{1,2\}m test.txt Output: room room
Saurabh Barjatiya
IIIT Hyderabad
Basics Miscellaneous
Contents
Basics Matching single character Using special characters Repetition match Miscellaneous OR of regular expressions Backreference
Saurabh Barjatiya
IIIT Hyderabad
Basics Miscellaneous
OR of regular expressions
AND on regular expressions can be performed by just concatenating two regular expressions. For OR of regular expressions | operator can be used. Example 16 - Usage of | Commands: grep -o s[a-z]\+\|[a-z]\+s test.txt Output: this shelves this
Saurabh Barjatiya
IIIT Hyderabad
Basics Miscellaneous
Backreference
Backreferences can be used to refer to matched regular expression when using regular expressions for search and replace operations. Example 17 - Usage of backreference Commands: sed s/\<i\([a-z]\+\)\>/I\1/g test.txt | grep I Output: there are two elfs In this room there are two shelves In this room
Saurabh Barjatiya
IIIT Hyderabad