In this article, we are going to learn basic Regular Expression.
Why Regular Expression(Regix)
Regular expression is one of the daily necessary items for a developer as well as tester. My focus will be on how to cover both side. I personally use regular expression
- Jmeter, Gatling, Load Runner for extracting information from response data/headers/cookie/url
- Data Scraping, for extracting information from any HTML/CSS/XML element
- Selenium webdriver, for finding element
- Robotium, for finding element
- Genie, For finding element.
- NodePadd++ to modify regex based information.
So, my focus will be the basic terms and examples which are used by me.
First, let’s have an idea about what is Regular Expression. A regular expression is the set of characters containing symbolic and literal meaning, together they create an expression that matches like as pattern match from a given character set. Detail description, you may find in wiki. The main objective is , getting objective character set from given set of characters. So, a regular expression activity will have 3 parts. a. Given text or set of characters or a doc( text/html/xml or any) b. Pattern to match( the regular expression) c. Matched Array(the result set)
Before start writing Expressions, we need to know some Stringdefination/meaning (literal) in the regex. Let’s use see in the rgular expression the meaning of the symbols.
- Digits :
- Capital Letters:
- any character:
- any single digit:
- any Non Digit character:
- white space:
- any Non White space:
- any alpha numeric character:
- any non alpha numeric character:
- starts with indicator:
- ends with indicator: ```$
- makes/captures in a groups:
- makes/captures in a sub groups:
- makes/captures variable content: (.*)
Conditional Logic expressions
- matches a or b:
- only one of a, b or c:
- Not of abc, means no a/b/c:
- any single character of range a to z:
- any single character of range A to Z:
- any single number between 0-9:
[0-9].It is similar to
- m times repetitions:
- Minimum m time to maximum n time repetitions:
- any literal present ZERO or more times :
- any literal present ONE or more times:
- optional character indication :
So, what are those. Those are meaning in the expression.
If our data string is like abcdefgh123ijklmnopqrstuvwxyz and we need to find 123 from there.
So, our regular expression will be simple
we can add some intelligence here. What if we do not know the value is 1,2 or 3. If we need 3 adjacent digits. So , our expression will be
As there is 3 times digits, we can write like
If, we consider a digit but there can 1 minimum and 3 maximum. Then expression will be
In a similar situation when we need min 1, maximum unlimited the expression will be
Or no minimum and unlimited maximum digits , the expression
- For learning basic with JAVA
- To practice
- To do experiment or test before apply , use this tool The Regex Coach
And for advanced users , you may see this. It’s a very enlarge support for JAVA.