REg-EX

(the bare minimum)

A train leaving a station at 10:50pm from San Francisco is traveling at 40MPH. Another train leaving Chicago at 12:00pm is going 60MPH.  At which point will the two trains cross paths.

ax + b = c

basic reg-ex symbols

  Characters
        cat matches 'cat' and 123 matches '123'
        10 is two symbols 1 and 0

  Parenthesis
      (cat) is the same as cat

  The Line Anchors: ‘^’ and ‘$’
        ^cat matches lines that start with blue 'cat pics'
        cat$ matches lines that end with cat 'bobcat'

  

       



BASIC REG-EX SYMBOLS

  The Character Class: ‘[]‘
        gr[ea]y matches 'grey' or 'gray'
        [0-9] matches 1,2,3....9 
        [^abc] matches anything except a,b,c  

  The dot
        .  matches any single character a,x,6,0 except line breaks 
        [.] or \. matches .
  
  The Alternation : ‘|’ 
        cat|dog matches cat or dog
        Mi(ke|chael) matches Mike or Michael

        
       


BASIC REG-EX SYMBOLS

 The Optional match: '?'
        flavou?r matches 'flavour' or 'flavor'

  Quantifiers: ‘+’ and ‘*’
        go+al means at least one 1  o      goal or gooooooooal
        go*al means 0 or more o    gal, goal or goooooooal    

  Interval Quantifier: ‘{}’
        go{1,3}al  matches goal, gooal, goooal but not gooooal or gal    
        go{0,1}al  === go?al  

  Escape character:  '\'
        what? matches wha but what\? matches what?

  

BASIC REG-EX SYMBOLS


\d   A digit: [0-9]
\D   A non-digit: [^0-9]
\s   A whitespace character: [ \t\n\x0B\f\r]
\S   A non-whitespace character: [^\s]
\w   A word character: [a-zA-Z_0-9]
\W   A non-word character: [^\w]
\b   Word boundary


HANDS ON

   Match user name
  • alphabets, numbers and _ allowed
  • must begin with any of those characters
  • must have 3 to 16 characters in total

  What does this match?
    ^#?([a-f0-9]{6}|[a-f0-9]{3})$

    Java endsWith, startsWith, contains



More hands on

A?B?
A|C
B

  Match days of the week (ex: Mon, Tue, Wed ... )

The Giants In Tiny Pants Problem


Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

(?:(?:rn)?[ t])*(?:(?:(?:[^()<>@,;:\".[] 000-031]+(?:(?:(?:rn)?[ t] )+|Z|(?=[["()<>@,;:\".[]]))|"(?:[^"r\]|\.|(?:(?:rn)?[ t]))*"(?:(?: rn)?[ t])*)(?:.(?:(?:rn)?[ t])*(?:[^()<>@,;:\".[] 000-031]+(?:(?:( ?:rn)?[ t])+|Z|(?=[["()<>@,;:\".[]]))|"(?:[^"r\]|\.|(?:(?:rn)?[ t]))*"(?:(?:rn)?[ t])*))*@(?:(?:rn)?[ t])*(?:[^()<>@,;:\".[] 000-0 31]+(?:(?:(?:rn)?[ t])+|Z|(?=[["()<>@,;:\".[]]))|[([^[]r\]|\.)*](?:(?:rn)?[ t])*)(?:.(?:(?:rn)?[ t])*(?:[^()<>@,;:\".[] 000-031]+ ... for 6343 characters ...

deck

By Pavan K Mutt

deck

  • 175