logo

NJP

Common Regular expressions and Cheat sheet

Import · Sep 20, 2020 · article

It is important to stress the distinction between greedy and not greedy ("lazy") operators. In general, greedy operators are overused, this may lead to surprises. [ addition thanks to @Daniel Oderbolz ]

Non-greedy operator:

/a+?a{2,}?/​

There are also these non-greedy operators available:

/a*?/  //match a between 0 and unlimited times (including no characters at all)
/a??/ //match a between 0 and 1 time

Ideally, one starts with the non-greedy variants and uses the greedy ones only if required.

Number of characters between x and y

You can check the number of characters by using regular expression. Using . (all characters except newline) and {x,y} Where x is the minimum amount and y the maximum.

Regular Expression

.{3,50}

Example

Between 3 and 50 characters

Number of characters exact length

Regular Expression

.{50}

Example

Must match 50 characters exactly

Email

Excepting all domains

Regular Expression

^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$

Example

firstname.lastname@example.com

Excepting specific domain

Replace domain in below.

Regular Expression

^([a-zA-Z0-9_\-\.]+)@domain\.com$

Example

firstname+lastname@domain.com

@Daniel Oderbolz adds that Email is really hard to check (the example does not allow this+email@gmail.com). To check emails, one almost must resort to using a library or a regex like http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html

Phone number

Start with 0 and total of 10 digits

Regular Expression

^[0]\d{9}

Example

0612345678

Alternative

Regular Expression

^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\./0-9]*$

Example

+(123) - 456-78-90

Dutch notation

Regular Expression

^[0]\d{9}

Example

+31(0)6 123 45678

URL

ftp/http/https. Provided by ServiceNow

Regular Expression

(((ftp|http|https):\/\/)|(www\.))([-\w\.\/#$\?=+@&%_:;]+)

Example

https;//www.google.com

ftp only

(((ftp):\/\/)|(www\.))([-\w\.\/#$\?=+@&%_:;]+)

https only

(((ftp):\/\/)|(www\.))([-\w\.\/#$\?=+@&%_:;]+)

http/https/www

Regular Expression

(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)

Example

www.google.com

IBAN

Regular Expression

^[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}([a-zA-Z0-9]?){0,16}$

Example

NL02ABNA0123456789

image Regex is unable to verify the check digit (the same goes for an ISBN). Instead use code adapted from Rosetta Code: https://rosettacode.org/wiki/IBAN#JavaScript

(thanks to @Daniel Oderbolz)

IP address

127.0.0.1

Regular Expression

^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$

Example

Accept:

127.0.0.1
192.168.1.1
192.168.1.255
255.255.255.255
0.0.0.0
1.1.1.01

Reject:

30.168.1.255.1
127.1
192.168.1.256
-1.2.3.4
1.1.1.1.
3...

Time

h:mm or hh:mm format

Regular Expression

[0-9]?[0-9]:[0-9][0-9]

Example

12:30 or 6:30

Zip code

Zip code validation Provided by ServiceNow

Regular Expression

^[0-9]{5}(?:-[0-9]{4})?$

Example

12345 or 12345-1234

Alternative

Regular Expression

\d{5}-\d{4}|\d{5}

Example

12345 or 12345-1234

Dutch notation

Regular Expression

\d{4}\w{2}

Example

Accepts 1234AB and 1234ab

Number

Number validation Provided by ServiceNow

Regular Expression

^[0-9]*$

Example

0 or more numbers, without dots or comma's like: 0, 99, 999999999

Percentage

number between 1 and 100

Regular Expression

^[1-9][0-9]?$|^100$

Example

Number between 1 and 100. Accepted minimum value is 1. Excepted maximum value is 100.

If you have any Regular Expressions you think others could use as well, please share and I will update this post.

Cheat sheet

Character classes
. any character except newline
\w\d\s word, digit, whitespace
\W\D\S not word, digit, whitespace
[abc] any of a, b, or c
[abc] not a, b, or c
[a-g] character between a & g
Anchors
abc$ start / end of the string
\b\B word, not-word boundary
Escaped characters
\.\*\\ escaped special characters
\t\n\r tab, linefeed, carriage return
Groups & Lookaround
(abc) capture group
\1 backreference to group #1
(?:abc) non-capturing group
(?=abc) positive lookahead
(?!abc) negative lookahead
Quantifiers & Alternation
a*a+a? 0 or more, 1 or more, 0 or 1
a{5}a{2,} exactly five, two or more
a{1,3} between one & three
a+?a{2,}? match as few as possible
ab\ cd

Source: https://regexr.com/

How to use

For instructions on how to use these Regular Expressions, please see:

https://community.servicenow.com/community?id=community_article&sys_id=ebcf2b6edbd7d0103daa1ea668961...

Additional resources by @Daniel Oderbolz

View original source

https://www.servicenow.com/community/developer-articles/common-regular-expressions-and-cheat-sheet/ta-p/2297106