RegExpTest - Build your regular expression online
A regular expression is a very powerful compact langage for string manipulation. It may be used to
- verify string formats in user input (email, enforce password policies ...)
- generically and easily separate and/or replace items in a string (parsing, placeholders ...)
Your regular expression pattern: (line breaks are removed)
or try a pattern template:
IP address v4
test ok
test ko
Email address (full RFC 822)
test ok
test ko
Match options:
Test string:
Regular expressions cheat sheet
• Special characters . * + ? ^ $ { [ ( | ) \ have special meaning and must be escaped with \ to be used without the special meaning : \. \* \+ \? \^ \$ \{ \[ \( \| \) \\
| . | any char, exactly 1 time |
| * | any char, 0-∞ times |
| + | any char, 1-∞ times |
| ? | any char, 0-1 time |
| ^ | start of string (or line if multiline mode) |
| $ | end of string (or line if multiline mode) |
| | | equivalent to OR (there is no AND, check the reverse of what you need for AND) |
See
here for more details.
• To create a named group (.NET specific), use (?<GroupName>Pattern)
Example to get the 4 digit groups of an IP address: (?<g4>\d{1,3})\.(?<g3>\d{1,3})\.(?<g2>\d{1,3})\.(?<g1>\d{1,3})
• To create a non capturing group (used to optimize resources needed for the computation), use (?:Pattern) instead of (Pattern) which is a capturing group
• Useful link: regular expression implementation in .NET
here
Regular expressions tricks
(. may be replaced by any pattern)
.*? finds the smallest interval matching the expression
.* finds the largest interval
.{8,} finds at least 8 times any char
.{8} finds exactly 8 times any char
.{8,12} finds between 8 and 12 times any char
[a-z] finds exactly one time any character between a and z. [a-zA-Z] may be used if ignore case is not used.
[4-6] finds exactly one time any character between 4 and 6.
RFC 822 regular expression is from http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html