This is a short little post with the goal of helping you to learn the elusive regular expressions!
This takes advantage of a built in regex feature in the Atom text editor. It may work in other text editors as well, so give it a try in your favorite text editor or IDE. If it doesn’t work, you can download Atom here (don’t worry, its free!).
First, create a new file in the Atom editor.
Next, type in a line of text. Here is the line I will be using
Next type CMD+f (Ctrl+f on Windows) to open the find and replace utility.
Then select the ‘regex’ option to treat the search item as a regular expression (see red arrow below).
Finally, highlight the line of text that you typed and press CMD+f (Ctrl+f on Windows). The search box should update to display your selection formatted as a regular expression.
Congratulations! You now know how to use Atom to build regular expressions for you!
Understanding the generated expression
Recall that the original string was
(Hello). [World!]. Parenthesis and brackets are special symbols in regex, so they must be escaped with the
\ character. Note that a period
. is also a special regex character. This is one of the most misused characters in regex, because a period by itself will match any character! Simply typing a
. to match a period in a string will match that string, but it will also match any string that contains any other character in place of the period.
This only works for one-line expressions. 🙁 Even magic has its limits :).
https://regexone.com/ – An awesome interactive tutorial for learning regular expressions.
https://regex101.com/ – A regex tester. You must know the regular expression, or try typing regular expressions until they match the example text you entered.