Regex

From NoskeWiki
Jump to: navigation, search

About

Regular Expression ("REGEX" for short) is a VERY powerful tool for matching string patterns(typically for find and replace) using advanced wildcard options. I'd highly recommend visiting the links (below) to learn more - can save you LOTS of code if you want to do advanced find & replace text operations.


Interactive Regex Webpages

  • regex101.com - a fantastic tool to paste in text and see what regex you need (plus you can create an account and save scripts).
  • regexr.com - pretty good.


Examples

\b[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}\b     // Matches any e-mail address.
\b[\w\d._%-]+@[\w\d.-]+\.[\w\d]{2,4}\b                 // Better version of above.
\d{2}-\d{5}                                            // Finds ID # in format: 2 digits, hyphen, 5 digits.
^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$      // Matches phone number (or just \d{8,10} for unformatted).


^\s*$                  // Matches a blank line.
"[\t ]+\n", "\n"       // Removes trailing whitespaces from sentences.


Symbols

Below is a summary of the most useful and common regex meta characters and special characters:

Regex special chars.gif

Regex metachars.gif




Links