The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Willem
Giga Sage
Giga Sage

Common Regular Expressions

Generic

greedy and non-greedy

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

 

⚠️ 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...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 match ab or 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 

 

 

Comments
Tom131
Mega Explorer

Very useful, thanks!

Dave Clemmer
Mega Guru

Simpler phone alternative:

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

Also improved to accept at most one '+' at the beginning.  Better would be to enforce a format, although I'm sure this was done to accept numbers from all over the world, not just from one country (doing that properly would be very difficult, I'm sure.  I don't really know the problem space there, though, so I couldn't really attempt it myself, outside of the US).

Simpler IP address:

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

Possibly better to cut down options to not accept broadcast and multicast addresses, but far more complicated.

Better time:

^(?:2[0-3]|[01]?\d):[0-5]\d$

This accepts 24-hour time, while rejecting hours > 23 or minutes > 59.  Better yet would be to add handlings for AM/PM and separate that from 24-hour time (but again, far more complicated).

Currency values (ignoring the currency symbol itself, of course):

^\d+(?:\.\d{2})?$

Those URL ones are a bit of a mess (they probably generate few false negatives, but there are lots of false positive possibilities), but that is an incredibly difficult problem, with lots of edge cases.

Daniel Oderbolz
Kilo Sage

Dear @Willem 
Thanks a lot for this great resource.

I would like to add some subtleties to this challenging topic and I hope I do not sound patronizing in doing so.

  • It is a common misconception that . matches all characters. In particular, it does not match newline, so a pattern using . may not exceed a single line.
  • 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. You added the non-greedy operators

 

/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.
This is especially relevant when using groups with ()

Willem
Giga Sage
Giga Sage

@Daniel Oderbolz thanks for these additions! I will update the article accordingly and credit you and your reply.
Always good when people join in and improve the overall content

Daniel Oderbolz
Kilo Sage

Hey @Willem that was quick!

Thanks a lot for taking the time to add this stuff  - great!

If we had some kind of Wiki functionality, we could even write such articles together...

(Remembering past days).

Daniel Oderbolz
Kilo Sage

To use it in a query (at least to my knowledge), you need to use GlideFilter as discussed here: GlideFilter and Regular Expression Match 
The performance is abysmal. There is an operator called MATCH_RGX, but in a normal condition builder it seems to be broken.

Elson Tan
Tera Contributor

hi ,
i have a catalog item required user to enter a 8-digits response code from another program.
The source response code have a space in between

 

eg 1234 5678

Is it possible to to have a expression to trim the white space in between when user copy and paste the above 8 digits with space ?
i have set my variable attribute = max_length=8,

so user need to manually delete the space, before he can enter the last digit.

Thank you

BuriB
Tera Expert

Hi, I need validation for Uppercase (capital-)letters and Digit only and max / min length 9 (Digit and Letters). Thank you in Advance 

Dave Clemmer
Mega Guru

@BuriB /[A-Z\d]{9}/ if they're commingled randomly.

 

Also, just noticed that the Dutch variant listed above isn't quite right; it will accept numbers and underscores for the last two characters.  Changing the \w to [a-zA-Z] or /\d{5}\w{2}/ to /\d{5}[a-z]{2}/i will fix it. 

Version history
Last update:
‎05-23-2023 08:56 AM
Updated by:
Contributors