Presentation is loading. Please wait.

Presentation is loading. Please wait.

Regular Expressions ashishFA@cse.

Similar presentations


Presentation on theme: "Regular Expressions ashishFA@cse."— Presentation transcript:

1 Regular Expressions

2 Where can I use Powerful in Text Editors – vi, editplus
- Search , search and replace, text processing Text Editors – vi, editplus Programming languages - perl, Java Grep,awk 2/18/2019Friday, January 28, 2005

3 Before RegEx Wildcard *.txt My_report*.doc
Here the * indicates any number of any characters. 2/18/2019Friday, January 28, 2005

4 ! Regular expressions (RegEx) tend to be easier to write than they are to read 2/18/2019Friday, January 28, 2005

5 What is RegEx a regular expression-- a pattern that describes or matches a set of strings Matched text– chunk of text which matches the regular expression. ca[trn] Matches car, can, cat Editplus is used throughout this presentation as tool to demonstrate the regular expressions 2/18/2019Friday, January 28, 2005

6 Structure of RegEx Made up of normal characters and metacharacters.
Metacharacters – special function $ ^ . \ [] \( \) + ? $ means end of line ^ start of line 2/18/2019Friday, January 28, 2005

7 Literal match RegEx: cat will match the word cat
It will also match words like concatenation , delicate, located, modification It is not desired sometimes ? solution 2/18/2019Friday, January 28, 2005

8 Matching Match the space before and after “cat” “ cat ”
? Still problem 2/18/2019Friday, January 28, 2005

9 Character class Want to search ‘in’ or ‘on’ ..
So searching RegEx : [io]n will match in and on both [ ] : used to specify a set of character to select from. [a-h] : indicates set of all characters from a to h [4-9A-T] 2/18/2019Friday, January 28, 2005

10 Character class It can also contain individual characters as : [acV5y0] [0-9] : ? [0-9][0-9] :? 18[0-9][0-9]:? 2/18/2019Friday, January 28, 2005

11 Example set of vowels set of consonents
[aeiou] set of consonents [bcdfghjklmnpqrstvwxyz] Consider matching words which start with 2 vowels and end with consonant [aeiou][aeiou][bcdfghjklmnpqrstvwxyz] ? “ [aeiou][aeiou][bcdfghjklmnpqrstvwxyz] ” 2/18/2019Friday, January 28, 2005

12 Negation The absence of any character or set of character can be shown using ^ symbol [^ab^8] : means not a , but b , but not 8 [^c-p] : means any character other than c..p “[^t]ion ” : select all words ending with ion but with not before it 2/18/2019Friday, January 28, 2005

13 Start/End of line ^ : indicates start of line
$ : indicates end of line Example: search lines starting with I Use RegEx : “^I ” search lines ending ending with is Use RegEx : “ is$” 2/18/2019Friday, January 28, 2005

14 match . : Any character match
e.e : match all strings where first letter is e and last is e. Try “ e.e ” If you want only words to be searched then change the query to e[a-z]e 2/18/2019Friday, January 28, 2005

15 Repeated match * : match the previous character or character-class zero or more times be* : will match sequence of zero or more ‘e’ preceded by b + : similar to * Only difference is that it matches sequence of one or more. 2/18/2019Friday, January 28, 2005

16 Selecting a number Single digit : [0-9]
When single digit is repeated zero or more times it is a number. (digit)repeat [0-9]* $[0-9]* : ? \$[0-9]* 2/18/2019Friday, January 28, 2005

17 Selecting a word Word is composed of alphabets A word is : [a-z]*
A word in all capital letters : ?? A word starting with capital letter :[ ][ ]* 2/18/2019Friday, January 28, 2005

18 Alternate match | : symbol is used to specify alternate match
Search: (above)|(below) 2/18/2019Friday, January 28, 2005

19 Search Day Words [a-z]*day “[a-z]+day ” - “[A-Z][a-z]+day ”
2/18/2019Friday, January 28, 2005

20 Escaping Special meaning
How to match (, ) or * To match the characters which are used as Metacharacter, ‘\’ is added before them as an escape character. i.e. to match ( write \( and to match period . write \. 2/18/2019Friday, January 28, 2005

21 Search patterns has, have, had not, n’t ((have)|(had)|(has))
(( )|(n't)|( not ))* ((have)|(had)|(has))(( )|(n't)|( not ))* 2/18/2019Friday, January 28, 2005

22 2/18/2019Friday, January 28, 2005

23 References Editplus help pages
OReilly - Mastering Regular Expressions Google “regular expression tutorial” 2/18/2019Friday, January 28, 2005

24 Thank you ! 2/18/2019Friday, January 28, 2005


Download ppt "Regular Expressions ashishFA@cse."

Similar presentations


Ads by Google