Regular Expressions

In a growing number of locations in FlexMail and FlexStream you can now use regular expressions. This page contains a collection of examples of often used patterns. You can use these examples as they are or modify them to suit your needs.

The panel at the right side provides some links to useful tools and sites that can help you in understanding and building regular expressions.


Examples

Below, you will find many example patterns that you can use for and adapt to your own purposes.

Filter a number that is 5 or 6 digits long
Expression: \d{5,6}
Description: Matches a number of 5 or 6 digits. In {x,y}, x specifies the minimum and y specified the maximum number of occurances of a digit (\d).
Matches: 12345 | 123456
Non-matches: 1234 | 1234567
Find a number after a specific text
Expression: (?<=Invoice Number:\s*)\d+
Description: '(?<= abcd)' is what is called a positive Lookbehind. It searches for 'Invoice Number:' followed by a number of white spaces (spaces, tabs, etc) without making it part of the match. '\d+' matches any number of digites after 'Invoice number:'
Matches: '12345' in 'Invoice Number: 12345'
Non-matches: 'Account: 12345'
Find a number before a specific text
Expression: \d+\.\d{2}(?=\s*DISCOUNT)
Description: '(?= abcd)' is what is called a positive Lookahead. It searches for 'DISCOUNT' preceded by a number of white spaces (spaces, tabs, etc) without making it part of the match. '\d+\.\d{2}' matches a number with 2 decimals.
Matches: '1.03' in '1.03 DISCOUNT'
Non-matches: '123 DISCOUNT' | '1.03 OFF'
Find a text between two texts
Expression: (?<=My cow\s*).+(?=\s*gives milk)
Description: '(?<= abcd)' is what is called a positive Lookbehind. It searches for 'My cow:' followed by a number of white spaces (spaces, tabs, etc) without making it part of the match. '(?= xyx)' is a positive Lookahead, which searches for 'gives milk', again without making it part of the match. '.+' matches all characters between 'My cow' and 'gives milk'.
Matches: 'always' in 'My cow always gives milk'
Non-matches: 'My goat always gives milk' | 'My cow always eats grass'
Simple e-mail validator
Expression: ^\w+[\w-\.]*\@\w+((-\w+)|(\w*))\.[a-z]{2,3}$
Description: Email validation. With this short expression you can validate for proper email format. It's short and accurate.
Matches: bob-smith@foo.com | bob.smith@foo.com | bob_smith@foo.com
Non-matches: -smith@foo.com | .smith@foo.com | smith@foo_com
Match last occurrence of a pattern
Expression: foo(?!.*foo)
Description: Finds the last occurrance of a specific pattern in an area. The key to the solution is a so called "negative lookahead". A lookahead doesn’t consume characters in the string, but only asserts whether a match is possible or not. So if you wanted to extract the last “foo” in the text “foo bar foo bar foo” your regex would look like 'foo(?!.*foo)'
Matches: Last 'foo' in 'foo bar foo bar foo'
Non-matches: bar dir get
Find European Currency string
Expression: ^[€£]?\s*(([0-9]{1,3}\.([0-9]{3}\.)*[0-9]{3})|[0-9]+)(,[0-9]{2})?$
Description: Matches currency input with or without thousand-separators, with or without decimals, and with or without Pound or Euro-sign (plus spaces).
Matches: € 3.023.123,34 | 9.876.453 | 123456,78
Non-matches: 4.33.234,34 | $1,234 | abc
Find US Currency string
Expression: ^\$?\s*(([0-9]{1,3},([0-9]{3},)*[0-9]{3})|[0-9]+)(\.[0-9]{2})?$
Description: Matches currency input with or without commas, with or without decimals and with or without $-sign (plus spaces).
Matches: $3,023,123.34 | 9,876,453 | 123456.78
Non-matches: 4,33,234.34 | $1.234 | abc
Find an US Address block in an area
Expression: (.*\n){2,4}.*\,\s*[A-Z]{2}\s+\d{5}(-\d{4})?
Description: Looks for an US address in an area. The address must be at least be 3 lines of which the last line sould consist of a city, followed by a comma (,) a state and a Zip5 or of zip5+zip4.
Matches: Agnes Brunette
5622 N Audubon St
Spokane, WA 12345-6789

Agnes Brunette
Suite 100
5622 N Audubon St
Spokane,WA 12345
Non-matches: Agnes Brunette
Spokane, WA 12345-6789

Agnes Brunette
5622 N Audubon St
Spokane
Validate US Postal Code
Expression: (?!00000)(?:(?:\d{5})(?:[ -](?=\d))?(?:\d{4})?)
Description: Validate US zip codes. Matches all zip codes of exactly 5 digits except 00000. Optionally, matches zip5+zip4 where zip5 is exactly 5 digits, zip4 is exactly 4 digits, and zip5 and zip4 are, optionally, separated by a single space or hyphen.
Matches: 12345 | 123456789 | 12345-6789
Non-matches: 12345- | 00000 | 00000-6789
Get City from US Address
Expression: (?<=\n).*(?=\,.*$)
Description: Retrieves the city from the last line of an US address. The city must be at the last line of the address and must be seperated by a comma from the state and post code.
Matches: Columbia in Columbia, SC 29210
Non-matches: Columbia SC 29210
Get the state from a US Address
Expression: (?<=\n*.*[,\s])([A-Z]{2})(?=(\s+\d{5}([\s-]+\d{4})?)?\s*$)
Description: Retrieves the (2 character) state abbreviation from a single address line or the last line of a US address block.
Matches: SC in Columbia, SC 29210
Non-matches: Columbia SC 29
Match extended characters
Expression: [a-zA-ZÀ-ÿ0-9]
Description: Matches: 1)all characters from a to z (lowercase invariants); 2)all characters from A to Z (uppercase invariants); 3) all characters from À to ÿ (all accent characers including French accents in uppercase and lowercase); 4) all digits from 0 to 9.
Matches: Frédérique
Non-matches:



Copyright © 1993 - 2024 Flex Systems B.V. Best viewed in Chrome, IE or FF. HTML5 Powered