9. Grammar of Regex * RE = one or more non-empty ‘ branches ‘ separated by ‘|’ Branch = one or more ‘ pieces ’ Piece = atom followed by quantifier Quantifier = ‘*,+,?’ or ‘ bound ’ Bound = atom{n}, atom{n,}, atom {m, n} Atom = (RE) or () or ‘ ^,$,’ or followed by `^.[$()|*+?{ or any-char or ‘ bracket expression ’ Bracket Expression = is a list of characters enclosed in `[ ]'
10. Meta Chars? 2 + 4 Here ‘+’ has some special meaning In a normal Expression like :
11. Meta Chars Quote the next metacharacter ^ Match the beginning of the line . Match any character (except newline) = [^] $ Match the end of the line (or before newline at the end) | Alternation ( ) Grouping [ ] Character class { } Match m to n times * Match 0 or more times + Match 1 or more times ? Match 1 or 0 times
12. Non-printable Chars tab (HT, TAB) newline (LF, NL) return (CR) form feed (FF) alarm (bell) (BEL) escape (think troff) (ESC) 33 octal char (example: ESC) 1B hex char (example: ESC) {263a} long hex char (example: Unicode SMILEY) K control char (example: VT) {name} named Unicode character
15. Shorthand Chars word character [A-Za-z0-9_] decimal digit [0-9] whitespace [ ] not a word character [^A-Za-z0-9_] not a decimal digit [^0-9] not whitespace [^ ]
16.
17.
18.
19.
20.
21.
22.
23.
24. Non Greedy Quantifiers {,}? *? +? ?? To make non greedy quantifiers append ‘?’ <.+?> My first <strong> regex </strong> test. <strong> Use negated classes <[^>]+> My first <strong> regex </strong> test. <strong>
25.
26.
27.
28.
29.
30.
31. Float number = integerpart.factionalpart Matching a float number Basic Principle – Split your task into sub tasks
32. Integerpart = + -> will match one or more digits Matching a float number
33. Matching a float number Literal dot = Integerpart = + -> will match one or more digits
34. Matching a float number Literal dot = Integerpart = + -> will match one or more digits Fractional part= + -> will match one or more digits
35. Integerpart = + Matching a float number Literal dot = Fractional part = + Combine all of them = ++
36. Matching a float number /++/ -> Is generic. It won’t match -123.45 or +123.45
37. Matching a float number /++/ -> Is generic. It won’t match -123.45 or +123.45 /[+-]?++/ -> will match.
38. Matching a float number But It won’t match - 123.45 or + 123.45 /[+-]?++/ -> will match. /[+-]? *++/ -> will match. But It won’t match 123. or .45
39. Matching a float number /[+-]? *(?:++|+|+)/ -> will match. But It won’t match 123. or .45 /[+-]? * (?: ++ | + | + ) /
40. Matching a float number /[+-]? *(?:++|+|+)(?:[eE]+)?/ -> will match. But It won’t match 10e2 or 101E5 / [+-]? * (?: ++ | + | + ) (?: [eE]+ )? /
41. Matching a float number /^[+-]? *(?:++|+|+)(?:[eE][+-]?+)?$/ -> will match. But It won’t match 10e-2 / ^[+-]? * (?: ++ | + | + ) (?: [eE][+-]?+ )? $/x
42. Match a float number /^ [+-]?* # first, match an optional sign (?: # then match integers or f.p. mantissas: ++ # mantissa of the form a.b |+ # mantissa of the form a. |+ # mantissa of the form .b |+ # integer of the form a ) (?:[eE][+-]?+)? # finally, optionally match an exponent $/x;
53. Inline modifiers & Comments Matching can be modified inline by placing modifiers. (?i) enables case-insensitive mode (?m) enables multiline matching for ^ and $ (?s) makes dot metacharacter match newline also (?x) ignores literal whitespace (?U) makes quantifiers ungreedy (lazy) by default $answers =~ / (?i) y (?-i) (?:es)?/ -> Will match ‘y’, ’Y’, ’yes’, ’Yes’ but not ‘YES’. Comments can be inserted inline using (?#) construct. /^ (?#begin) + (?#match integer part) (?#match dot) + (?#match fractional part) $/