Cool RegEx Examples

Cool RegEx Examples

What is RegEx?

Regular expressions are useful in search and replace operations. The typical use case is to look for a sub-string that matches a pattern and replace it with something else.

Sometimes, learning how to write RegEx’es before you even need them can save you a lot of time.

Here are some cool RegEx examples I learned;

  1. RegEx to find only empty lines

    1
    ^(?:[\t ]*(?:\r?\n|\r))+
  2. RegEx to find lines longer than 10 chars

    1
    [^\n]{10,}
  3. RegEx to match IPv4 Address

    1
    ^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$
  4. RegEx to export src part of HTML image tag

    1
    ^<\s*img[^>]+src\s*=\s*(["'])(.*?)\1[^>]*>$

… will be updated …