050-730 practice question 4 of 25

Using regular expressions, which matches ‘a’ or ‘b’?

Full set: 117 questions
Answer options
A: “a\b”
B: “a.b”
C: “a*b”
D: “a|b” Correct answer
E: “a:b”
Correct Answer:
D. “a|b”
Explanation

In regular expressions, the vertical bar | acts as an alternation operator, which is equivalent to a logical "OR". The expression a|b instructs the regular expression engine to match either the character or sub-expression on its left (a) or the character or sub-expression on its right (b). This is the standard syntax for matching one of several alternatives. Therefore, a|b is the correct expression to match either the character ‘a’ or the character ‘b’.

Why Incorrect

A. “a\b”: The \b is a word boundary anchor. This expression matches the character 'a' only when it is at the end of a word.
B. “a.b”: The dot . is a wildcard that matches any single character. This expression matches a three-character string starting with 'a' and ending with 'b'.
C. “ab”: The asterisk is a quantifier meaning "zero or more" of the preceding element. This matches strings like 'b', 'ab', 'aab', and so on.
E. “a:b”: The colon : is treated as a literal character. This expression matches only the exact three-character string "a:b".

References

1. Micro Focus UFT One Help Center: In the "Regular Expression Character and Operator Reference," the | character is defined as: "Matches either the expression before or the expression after the |. For example, z|food matches z or food." This directly confirms the use of | for alternation. (Source: Micro Focus UFT One 2023 Help Center, Section: "Regular Expression Character and Operator Reference").

2. Micro Focus LoadRunner Professional Help Center: The documentation on "Regular Expression Syntax" specifies that LoadRunner uses Perl 5 compatible regular expressions. It defines | as the alternation operator: "Matches one of a series of patterns. For example, cat|dog matches cat or dog." (Source: Micro Focus LoadRunner Professional 2023 Help Center, Section: "Vuser Scripts > Regular Expressions in VuGen > Regular Expression Syntax").

3. MIT OpenCourseWare: In the course materials for 6.035 Computer Language Engineering, regular expression operations are formally defined. The alternation (or union) operator | is specified as r1 | r2, which denotes a match for a string in the language of r1 or a string in the language of r2. (Source: MIT OCW, 6.035 Computer Language Engineering, Spring 2010, Lecture 2 Notes, Section 2.2 "Regular Expressions").

Scroll to Top