Want Premium Content Without Advertisement

Selenium Master Forum has monthly impressions about 500, 000 worldwide. To better serve test automation professionals with premium contents without advertisement on this site, we need to raise about $50,000 to support content writer and code developers. If every visitor donates $5 (five dollars), we can raise $50,000 very quickly, and remove all advertisement on this site. Please donate for the good cause. Once we raise the fund, we will completely remove advertisement. Thank you very much for your support in advance.  


us zip codes regular expression validation

US Zip Code Regular Expression Validation

In some tests, users need to test valid US zip codes by using regular expressions. Zip code validation may be needed when locating elements in the html tag or on the UI. This article explains how to validate US zip code regular expression pattern in Java code.  The most commonly used zip code pattern is:

Regular Expression Pattern: \b[0-9]{5}(?:-[0-9]{4})?\b

Explanation: (explanation syntax is from http://regex101.com)

Here is the Java source code to validate the US Zip Code regular expression


Validate dynamic Html Id with Regular Expression

Dynamic Id Regular Expression Validation

In some web applications, a html tag Id is dynamically generated for each user session. When using Selenium to identify the element, using locators such as Id, name, xpath may not work because the dynamic Id pattern changes constantly.

One option would be to use Regular Expression in Xpath or with other locators and locate the element. Before creating a generic regular expression, you need to validate the Regular Expression to make sure that the pattern of the regular expression covers all the possible scenarios. For example, in the user registration form below, the id changes and it has a pattern like "userId_mm_dd_yyyy_{5 digit random number}. We can use this pattern below when finding the userId Element. 


Here is the Java code to validate regular expression for this pattern. 


Regular Expression Reference

Regular Expression Reference

In test automation, sometimes, we encounter some dynamic element id, name or other attributes in the DOM. To identify the elements, we can use regular expressions. Below is the regular expression reference.


[abc] A single character of: a, b, or c
[^abc] Any single character except: a, b, or c
[a-z] Any single character in the range a-z
[a-zA-Z] Any single character in the range a-z or A-Z
^ Start of line
$ End of line
\A Start of string
\z End of string
. Any single character
\s Any whitespace character
\S Any non-whitespace character
\d Any digit
\D Any non-digit
\w Any word character (letter, number, underscore)
\W Any non-word character
\b Any word boundary
(...) Capture everything enclosed
(a|b) a or b
a? Zero or one of a
a* Zero or more of a
a+ One or more of a
a{3} Exactly 3 of a
a{3,} 3 or more of a
a{3,6} Between 3 and 6 of a

For example, if we need to find an email address " This email address is being protected from spambots. You need JavaScript enabled to view it. " , we can use the regular expression below. 

Regular Expression:  [a-zA-Z0-9]{1,}[@][a-zA-Z0-9]{1,}[.][a-zA-Z0-9]{3}