Saturday, November 22, 2014

Regular Expressions and some useful Examples.

Regular Expressions

Manytimes, while creating a regular expression in Real Testing Environment, we face some problems. Here, I have ried to extract out regular expressions, which we can apply directly as it is. Some of these scenarios are:
Pattern for Password
Pattern: [a-zA-Z]\w{3,14}
Matching Text: abcd, aBc45DSD_sdf, password
Non-Matching Text afv, 1234, reallylongpassword
The password's first character must be a letter, it must contain at least 4 characters and no more than 15 characters and no characters other than letters, numbers and the underscore may be used.
Pattern for IP Address
Pattern: \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}
Matching Text:  match any IP address just fine, but will also match 999.999.999.999 as if it were valid IP address.
Pattern for Visa Credit Card
Pattern: ([4]{1})([0-9]{12,15})
Matching Text: 4125632152365, 418563256985214, 4125632569856321
Non-Matching Text: 3125652365214, 41256321256, 42563985632156322
Validate against a visa card number. All visa cards start with a 4 and are followed by 12 to 15 more numbers.
Title Alphanumeric
Pattern: [a-zA-Z0-9]+$
Matching Text: 10a, ABC, A3fg
Non-Matching Text: 45.3, this or that, $23
Matches any alphanumeric string (no spaces).
Pattern for Six Integer Indian ZIP Code
Pattern \d{6}
Matching Text: 333333, 555555, 234455
Non-Matching: Text abcd, 1324, as;lkjdf
Matches 6 numeric digits, such as a zip code.
Pattern for Indian Currency
Pattern: \INR(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?
Matching Text: INR0.84, INR123458, INR1,234,567.89
Non-Matching Text: 12345, 1.234INR
This matches Indian currency format with INR as Ruppee sign. INR value must have at least one digit and may or may not be comma separated. Paisa value is optional.
 Matching a Valid Date
19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01]) matches a date in yyyy-mm-dd format from between 1900-01-01 and 2099-12-31, with a choice of four separators. The year is matched by (19|20)\d\d.

No comments:

Post a Comment