"Regular expressions" is eventually a popular search tool used by the programmers to find, validate, modify, or edit information. This concept of regular expressions can be used by the test engineers in the testing process for smart testing.
There would be many instances where a tester had to find and manipulate text. For example large volumes of text in a requirements document or manipulating thousands of the test cases. This can be done manually by using the “find” feature. But there are many pitfalls like only one string can be found at a time and cannot get the count of the strings. Strings having particular pattern for example pin, pan and pun cannot be searched. This consumes lot of time which in turn affects the productivity of the testing process.
Test engineer can complete the task more intelligently and increase the productivity (completing a task in less time very effectively and efficiently is said to be productive) by using a pattern language which is known as “Regular Expressions”.
Regular expression is a string that is used to describe or match a string according to certain syntax rules. It is a pattern language; any thing which has form of occurrence is a pattern. They are powerful wild-card text-processing tools. They give a concise description of a set, without having to list all elementsEg: a set containing 3 stringsHandel, ha:ndel and haendel can be described by the pattern “H(a: ae?)ndel”Eg: a set containing 3 stringsPin, pan and pun can be described by the pattern “P.n”.Regular expression is often shortened in speech to "Regex", and in writing to "Regexp" or "Regex".
The “Regular expressions” is a declarative notation that includes a powerful pattern language. Supported by all major databases, scripting languages, and programming languages, advanced editor’s support it, innumerous commercial and free libraries exists. Regex engine is a piece of software that can process regular expressions trying to match the pattern to the given string. Spaces are also used in patterns. Regex are case sensitive
Rules or syntax to be followed:
- Any character matches itself as long as it is not a special character (and special character is nullified with “\”)
- ”.” (Period) any single character.Eg (a.c) this means “a” followed by any single character, followed by “c”.
- ”-“is used to define ranges.Eg: ab(2,4)cà “a” followed by 2 to 4 “b’s” and followed by “c”
- ”^” (start) matches the start of the line (or any line, when applied in multiline mode)Eg ^(abc)à abc at the beginning of the string.
- ”$”(end) Matches the end of the line (or any line, when applied in multiline mode)Eg: $(abc)à abc at the end of the string“^” and “$” are known as anchoring characters.
- ”” (either or)eg: (abbc) this will match “ab” or “bc”
Scenarios where a test engineer can use “Regex”.
1· Test for a pattern within a string.For example, you can test an input string to see if a telephone number pattern or a credit card number pattern occurs within the string. This is called data validation.
2· Replace text.You can use a regular expression to identify specific text in a document and either remove it completely or replace it with other text.
3· Extract a substring from a string based upon a pattern match.You can find specific text within a document or input field.
Regex are greedy by default and try to match maximum number of strings. This can be a significant problem. But modern regular expression tools allow a quantifier to be specified as non-greedy, by putting a question mark after the quantifier.
Test engineers are reluctant to regular expressions as they find difficulty in writing the syntax. But a smart test engineer would know and learn to craft powerful time saving expressions and does testing effectively and efficiently.
Sources:
Ashwinpalaparthi
wikipedia.com
External Links:
Regular Expressions info
String tools .com
Regular Expression library
Regex Studio
No comments:
Post a Comment