Friday, October 23, 2015

Keynote: Regexp Short Notes

Regular expressions

Regular expression is a pattern describing a certain amount of text. So, Regexps are quite useful whatever programming language u're using.

Regexp Basics

Regular Expression Patterns :

  • . - any char;[A-Za-z] - any char within brackets;
    [^abc] - any char except ones*("abc")* within brackets;
    ( ) - marked subexpression.

  • * - zero or more instances; ? - zero or one time; + - one or more times.

  • \n - matches nTh match;
    {m,n} - matches at least m and not more than n times.

  • | - alternation, matcher either the expression before or after operator. cat|dog matches "cat" or "dog".

  • ^ - start position within string/line; $ - end position within string/line.

  • \s - whitespace; \S - anything but whitespace; \d/\D - digit/not digit.

  • \w/\W alphabetic char & _ symbol / excluding any alphabetic char & _ symbol.


Regexp Lookarounds

Look between excluding

Use [^u] where u is the symbol that will be excluded from search.

Grab everything between A & C but not include those:
pattern: \[^A].*[^C]\g
text1: AABBDDCC
text2: AEABBDDCEC

Lookahead-positive

Matches a group after the expression without including it in the result.
q(?=u) grabs q only if it's immediately followed by u.

Grab only B, which is followed by D:
pattern: \B(?=D)\g
text: AABBDDCC

Lookahead-negative

Specifies a group that can not match after the expression.
q(?!u) grabs q only if it's not immediately followed by u.

Grab only B, which is not followed by D:
pattern: \B(?!D)\g
text: AABBDDCC

Lookbehind-positive & Lookbehind-negative

Accordingly (?<=u)q & (?<!u)q work in opposite way that lookahead ones.
So, (?<=u)q matches q only if u precedes it.

*Lookbehind is not supported in JavaScript


see Also

1 comment: