Java Lect 17

download Java Lect 17

of 24

Transcript of Java Lect 17

  • 7/25/2019 Java Lect 17

    1/24

    Regular ExpressionsRegular Expressions

    ashishFA@cseashishFA@cse

  • 7/25/2019 Java Lect 17

    2/24

    01/16/16Frida Januar 2

    Where can I useWhere can I use

    Powerful in- Search , search and replace, text processing

    ext Editors ! "i, editplus Progra##ing languages - perl, $a"a

    %rep,aw&

  • 7/25/2019 Java Lect 17

    3/24

    01/16/16Frida Januar 3

    'efore RegEx'efore RegEx

    Wildcard! ()txt

    ! *+report()doc

    ere the ( indicates an+ nu#.er of an+characters)

  • 7/25/2019 Java Lect 17

    4/24

    01/16/16Frida Januar 4

    //

    Regular expressions (RegEx) end o

    !e easier o "rie #an #ey are oread

  • 7/25/2019 Java Lect 17

    5/24

    01/16/16Frida Januar 5

    What is RegExWhat is RegEx

    a regular expression-- a pattern that descri.es or#atches a set of strings

    *atched text! chun& of text which #atches theregular expression)

    ca0trn1! *atches car, can, cat

    Editplus is used throughout this presentation as tool to de#onstrate theregular expressions

  • 7/25/2019 Java Lect 17

    6/24

    01/16/16Frida Januar 6

    StructureStructure of RegExof RegEx

    *ade up of nor#al charactersand#etacharacters)

    *etacharacters ! special function

    23 ) 4 01 45 46 7 8

    2#eans end of line

    3start of line

  • 7/25/2019 Java Lect 17

    7/24

    01/16/16Frida Januar $

    9iteral #atch9iteral #atch

    RegEx: cat will #atch the word cat

    It will also #atch words li&econcatenation , delicate, located,#odification

    It is not desired so#eti#es 8

    ! solution

  • 7/25/2019 Java Lect 17

    8/2401/16/16Frida Januar 8

    *atching*atching

    *atch the space .efore and after;caton? )) So searching RegEx : 0io1n will #atch in

    and on .oth

    0 1 : used to specif+ a set of character toselect fro#)! 0a-h1: indicates set of all characters fro# a to

    h

    ! 0-A-1

  • 7/25/2019 Java Lect 17

    10/2401/16/16Frida Januar 10

    =haracter class=haracter class

    It can also contain indi"idualcharacters as : 0acBC+D1! 0D-1: 8

    ! 0D-10D-1:8! 0D-10D-1:8

  • 7/25/2019 Java Lect 17

    11/2401/16/16Frida Januar 11

    Exa#pleExa#ple

    set of "owels! 0aeiou1

    set of consonents! 0.cdfghG&l#npHrst"wx+1

    =onsider #atching words which start with J"owels and end with consonant! 0aeiou10aeiou10.cdfghG&l#npHrst"wx+1 8

    ! ;0aeiou10aeiou10.cdfghG&l#npHrst"wx+1