Regular expressions Flashcards
What are regular expressions used for?
a) To perform mathematical calculations
b) To verify and match strings according to patterns
c) To handle exceptions in Java
d) To store complex data structures
b) To verify and match strings according to patterns
What is the main purpose of the Pattern class in Java?
a) To hold string values
b) To store regular expression patterns
c) To concatenate multiple strings
d) To sort an array of strings
b) To store regular expression patterns
In a regular expression, what does [0-9]{3} represent?
a) Three alphabetical characters
b) Three numeric digits
c) A mix of letters and numbers
d) Any three symbols
b) Three numeric digits
Why is the dash character escaped in a regular expression pattern like \-?
a) To indicate it’s a literal dash rather than a special character
b) To allow it to match any character
c) To make it optional in the pattern
d) To denote the start of a new line
a) To indicate it’s a literal dash rather than a special character
What method is used on a Matcher object to check if a pattern exists within a string?
a) match()
b) find()
c) check()
d) verify()
b) find()
What does the [B|R] pattern check for in a regular expression?
a) Either a B or R as the first character
b) Any letter between B and R
c) Only lowercase b or r
d) A space or a new line character
a) Either a B or R as the first character
In the context of regular expressions, what is the purpose of the method matchName()?
a) To compile a pattern from a string
b) To check for matches and print a greeting based on the result
c) To convert all text to uppercase
d) To find and replace characters in a string
b) To check for matches and print a greeting based on the result
What does the regular expression [Vic|][[T|t]oria] aim to match?
a) Strings starting with Vicky only
b) Variations of the name Victoria, including nicknames
c) Any name ending with ria
d) All names beginning with T
b) Variations of the name Victoria
What is the primary advantage of using regular expressions over contains(), endsWith(), and startsWith()?
a) Regular expressions are faster
b) They allow more flexible and abstract pattern matching
c) They ignore case sensitivity automatically
d) They are built only for numerical data
b) They allow more flexible and abstract pattern matching
In regular expressions, what does the pipe symbol | represent?
a) A literal character
b) An alternative or choice
c) The end of a string
d) A space character
b) An alternative or choice