java_lect_17.ppt

24
Regular Regular Expressions Expressions

Upload: chander-kumar

Post on 11-Sep-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

  • Regular Expressions

    Regular Expression

  • *Friday, January 28, 2005*Where can I usePowerful in- Search , search and replace, text processing

    Text Editors vi, editplusProgramming languages - perl, JavaGrep,awk

  • *Friday, January 28, 2005*Before RegExWildcard*.txtMy_report*.doc

    Here the * indicates any number of any characters.

  • *Friday, January 28, 2005*!Regular expressions (RegEx) tend to be easier to write than they are to read

  • *Friday, January 28, 2005*What is RegExa 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

  • *Friday, January 28, 2005*Structureof RegExMade up of normal characters and metacharacters.Metacharacters special function $ ^ . \ [] \( \) + ? $ means end of line^ start of line

  • *Friday, January 28, 2005*Literal matchRegEx: cat will match the word cat

    It will also match words like concatenation , delicate, located, modificationIt is not desired sometimes ? solution

  • *Friday, January 28, 2005*MatchingMatch the space before and after cat cat ? Still problem

  • *Friday, January 28, 2005*Character classWant 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]

  • *Friday, January 28, 2005*Character classIt can also contain individual characters as : [acV5y0][0-9] : ?[0-9][0-9] :?18[0-9][0-9]:?

  • *Friday, January 28, 2005*Exampleset of vowels [aeiou] set of consonents [bcdfghjklmnpqrstvwxyz]

    Consider matching words which start with 2 vowels and end with consonant [aeiou][aeiou][bcdfghjklmnpqrstvwxyz] ? [aeiou][aeiou][bcdfghjklmnpqrstvwxyz]

  • *Friday, January 28, 2005*NegationThe 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

  • *Friday, January 28, 2005*Start/End of line^ : indicates start of line$ : indicates end of lineExample: search lines starting with I Use RegEx : ^I search lines ending ending with is Use RegEx : is$

  • *Friday, January 28, 2005*match. : Any character matche.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 toe[a-z]e

  • *Friday, January 28, 2005*Repeated match* : match the previous character or character-class zero or more timesbe* : will match sequence of zero or more e preceded by b+ : similar to *Only difference is that it matches sequence of one or more.

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

  • *Friday, January 28, 2005*Selecting a wordWord is composed of alphabetsA word is : [a-z]*A word in all capital letters : ??A word starting with capital letter :[ ][ ]*

  • *Friday, January 28, 2005*Alternate match| : symbol is used to specify alternate matchSearch: (above)|(below)

  • *Friday, January 28, 2005*SearchDay Words [a-z]*day[a-z]+day - [A-Z][a-z]+day

  • *Friday, January 28, 2005*Escaping Special meaningHow 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 \.

  • *Friday, January 28, 2005*Search patternshas, have, had not, nt

    ((have)|(had)|(has))(( )|(n't)|( not ))*((have)|(had)|(has))(( )|(n't)|( not ))*

  • *Friday, January 28, 2005*

  • *Friday, January 28, 2005*References

    Editplus help pages

    http://gnosis.cx/publish/programming/regular_expressions.html

    OReilly - Mastering Regular Expressions

    Google regular expression tutorial

  • *Friday, January 28, 2005*Thank you !