This example demonstrates how you can generate a regular expression that matches two or more unrelated bits of text in a file, regardless of where or how many times each bit of text occurs in the file. You can find this example as "Fields: alternation single" in the RegexMagic library.
For this example, we'll create a regex to finds all numbers and all email addresses in a file. To do this, we create a regex that matches a number or an email address. When you repeat this regex using the "find all" command in an application or programming languages, you will get a list of all the numbers and email addresses.
My favorite number is 42. You can email me at joe@fortytwo.com. Other nice numbers are 17, 382, and 794. joefortytwo@gmail.com is my alternative email address.
. RegexMagic automatically detects the correct "integer" pattern for this field.
with "kind of field" set to "alternation". The field with the integer pattern becomes field
, which is the first alternative under field
. The new field
is added as the second alternative under field
. RegexMagic automatically detects the correct "email address" pattern for this field.
# 1. One of the fields 2 to 3 # 2. Integer [0-9]+ | # 3. Email address [!#$%&'*+./0-9=?_`a-z{|}~^-]+@[.0-9a-z-]+\.[a-z]{2,63}
Required options: Case insensitive; Free-spacing.
Unused options: Dot doesn’t match line breaks; ^$ don’t match at line breaks; Numbered capture.
My favorite number is 42. You can email me at joe@fortytwo.com. Other nice numbers are 17, 382, and 794. joefortytwo@gmail.com is my alternative email address.
The generated regular expression uses alternation to combine the patterns for all the fields into one regular expression. The result is that this regular expression will match either a number, or an email address, regardless of context. To find all numbers and email addresses in a file, apply this regular expression repeatedly to the same text using a "find all" command in your application or programming language.