CIS-SM practice question 20 of 25

Which one of the following Regular Expressions could be used to parse "San Diego" from the string "Location San Diego"?

Full set: 106 questions
Answer options
A: Location (\W) (\w)
B: Location (L*)
C: Location (\w+ \w+) Correct answer
D: Location (W)
Correct Answer:
C. Location (\w+ \w+)
Explanation

The regular expression Location (\w+ \w+) is designed to find the literal string "Location " followed by a pattern that captures a two-word phrase. The \w+ component matches one or more "word" characters (alphanumeric characters plus underscore), which correctly identifies "San" and "Diego" individually. The space between the two \w+ tokens matches the literal space. The parentheses () around the pattern \w+ \w+ create a capturing group, which isolates and extracts "San Diego" from the full string.

Why Incorrect

A. Location (\W) (\w): This expression captures a single non-word character (the space) followed by a single word character ("S"), not the intended "San Diego".
B. Location (L): This expression looks for zero or more occurrences of the literal character 'L' after "Location ", which does not match the space in "Location San Diego".
D. Location (W): This expression looks for the literal uppercase character 'W' after "Location ", which is not present in the source string. ---

References

1. ServiceNow Developer Documentation, "Regular expression support in scripts": This official guide details the regular expression syntax used in ServiceNow server-side scripts, which is based on Java's regex engine. It defines \w as a predefined character class for a word character [a-zA-Z0-9] and + as a greedy quantifier for one or more matches. This directly supports the use of \w+ to match "San" and "Diego". (ServiceNow Developer Site -> API Reference -> Scripting in ServiceNow -> Server-side script basics -> Regular expression support in scripts).

2. ServiceNow Product Documentation, "Create a parsing rule": In features like Log Analytics, parsing rules are used to extract data from log lines using regular expressions. The documentation provides examples of using capturing groups () with quantifiers like + to extract variable-length strings, which is the principle applied in the question. (ServiceNow Product Documentation, Vancouver -> IT Operations Management -> Log Analytics -> Create a parsing rule, Section 2.d).

3. Stanford University, CS103 Courseware, "Guide to Regular Expressions": University courseware on automata theory provides foundational knowledge of regular expressions. These guides define the core components used in the question, explaining that \w is a shorthand for a class of characters and + is a quantifier for "one or more repetitions," validating the construction of the correct answer from first principles. (Reference: Stanford CS103: Mathematical Foundations of Computing, Handout #19, "Guide to Regular Expressions," pp. 1-2).

Scroll to Top