1. Introduction #
Regular expressions are strings that describe a search pattern that can be used in many situations. These can be used to quickly find what you are looking for, for example in a text or selector. In cases with text, it can sometimes be easier to create several small regexes that specify the variable each time, rather than creating a long complicated expression.
Name | Note | Link |
Course | Free course with exercises for regex | https://regexone.com/ |
Tests | Testing tool where you can test your regex against the data it manages | http://regexstorm.net/tester |
Cheat sheet | Quick reference sheet for regex | https://www.cheatography.com/davechild/cheat-sheets/regular-expressions/ |
1.1 Useful expressions #
Description | Expression |
Common time formats 00:00:00 and similar | (?:(?:([01] ?\d|2[0-3] ):)?([0-5] ?\d):)?([0-5] ?\d)(?:[000-999] ) |
Capture currency in different formats, DK 1.200,36 | USA 1,200.36 | \-?\d+[\.\,\d]* |
For email addresses | (?:[a-z0-9!#$%&’*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&’*+/=?^_`{|}~-]+)*|”(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\ x0e-\x7f])*”)@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9] ?[0-9] ))\.){3} (?:(2(5[0-5] |[0-4][0-9])|1[0-9][0-9]|[1-9] ?[0-9] )|[a-z0-9-] *[a-z0-9] :(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\]) |
Match on line breaks | [\r\n]+ |
Case insensitive | (?i)Match(?-i) |
Get filename from path without file extension | [ \w-]+?(?=\.) |
Search for text and extract the text on the line below | (?< =Keyword[\s\r\n]+.*)([^\r\n]+) |
Search for text and extract text behind it | (?< = Keyword +.*)([a-zA-Z0-9\ .\s] +) |
Search for text, and extract only numbers behind it, including periods and commas | (?< =Keyword .*)\-?\d+[\.\,\d]* |
Match up to a stop, such as a comma | ^[^,]* |
2. Examples #
This example can search for something specific, 6057 in this case, and extracts the first number/currency that comes after it.
(?< =6057.*)\-?\d+[\.\,\d]*
An output from KMD Aktiv where we’d like to get the CPR numbers (danish personal identification number), for example.
Regex = \d{6} -\d{4}
You can also search for something specific and only copy the text that follows it via (?< = SEARCH WORDS .*)
This way you can possibly extract something and create a new parse text with its output with a new regular expression, rather than creating a complicated expression. In the next part of the example here, you can create another Parse Text where you want to use (?< =af.*)\S\d* which extracts the number that comes after the text.