Download presentation
Presentation is loading. Please wait.
1
Selenium WebDriver Web Test Tool Training
Portnov Computer School Selenium WebDriver Web Test Tool Training Test Automation For Web-Based Applications Presenter: Ellie Skobel
2
Matching Text Patterns
Day 5 Matching Text Patterns
3
Patterns Allow the user to describe the expected text, via the use of special characters. Frequently user to check dynamic web content Often utilized to parse output, and/or to extract specific data from a larger chunk of text
4
Regular Expressions: Much more versatile then Unix style pathname pattern expansion (GLOB) Many special characters and character grouping options supported. MATCH . any single character [ ] character class: any single character that appears inside the brackets * quantifier: 0 or more of the preceding character (or group) + quantifier: 1 or more of the preceding character (or group) ? quantifier: 0 or 1 of the preceding character (or group) {1,5} quantifier: 1 through 5 of the preceding character (or group) | alternation: the character/group on the left or the character/group on the right ( ) grouping: often used with alternation and/or quantifier
5
Common Matching Patterns
Regular Expression Description [a-z] Ranges: matches a single alphabetical character [0-9] Ranges: matches a single digit [aeiou] Ranges: matches a single vowel ^regex Finds regex that must match at the beginning of the line. regex$ Finds regex that must match at the end of the line. [^abc] When a caret appears as the first character inside square brackets, it negates the pattern.
6
Meta Characters Regular Expression Description \d
Any digit, short for [0-9] \D A non-digit, short for [^0-9] \s A whitespace character, short for [ \t\n\x0b\r\f] \S A non-whitespace character, short for [^\s] \w A word character, short for [a-zA-Z_0-9] \W A non-word character [^\w] \b Matches a word boundary where a word character is [a-zA-Z0-9_]
7
REGEXP Examples regexp: .*[0-9]{1,2}:[0-9]{2} [ap]m regexpi: ^[a-z]+$
Will match 12 hour time format hh:mm (ONLY) regexpi: ^[a-z]+$ Will match any 1 word in lower, upper, or mixed case. regexp: .* thinks (dogs|cats|fish) make great pets. 00:00 am HELLO Antidisestablishmentarianism cheese 09377 thinks dogs make great pets! Alice thinks fish make great pets. NOBODY thinks cats make great pets?
8
Using Regular Expressions
Strings in Java have four built-in methods for regular expressions matches() split() replaceFirst() replaceAll() * The replace() method does NOT support regular expressions.
9
Java Methods Method Description str.matches("regexPattern")
Evaluates if "regexPatter" matches str. Returns true only if the WHOLE string can be matched. str.split("regexPattern") Creates an array with substrings of str divided at occurrence of "regexPattern" str.replaceFirst("regexPattern"), "replacementStr" Replaces first occurance of "regexPattern" with "replacementStr. str.replaceAll("regexPattern"), "replacementStr" Replaces all occurrences of "regexPattern" with "replacementStr.
10
Optimized Regex Matching
For more complex regular expressions or larger data size the java.util.regex.Pattern and java.util.regex.Matcher classes are used, which are optimized for performance. First create a Pattern object which defines the regular expression. The Pattern object is used to create a Matcher object for a given string. The Matcher object is then used to perform regex operations on a String.
11
Code Example import java.util.regex.Matcher; import java.util.regex.Pattern; ... String EXAMPLE_TEST = "This is my small example string which I'm going to use for pattern matching."; … Pattern p = Pattern.compile("\\w+",Pattern.CASE_INSENSITIVE); Matcher matcher = p.matcher(EXAMPLE_TEST); while (matcher.find()){ System.out.println(matcher.group()); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.