Py Flashcards
(16 cards)
What is the regex pattern to find any three-character sequence where the first character is ‘c’ and the last character is ‘t’?
Dot . - Wildcard character
c.t
C, The middle character can be anything, t
What test sentence can be used to demonstrate the regex pattern ‘c.t’?
I found a cat, a cot, and a cut in the room.
Expected matches: cat, cot, cut
What does the dot ‘.’ represent in regex?
Any single character (except newline)
It finds sequences where the first and last characters are separated by any character
What is the regex pattern to find strings that start with ‘Py’?
^Py
The caret ^ ensures that the match must occur at the start of the string or line
What test sentence can be used to demonstrate the regex pattern ‘^Py’?
Python is fun
Expected matches: [Py] from ‘Python’ at the beginning of the sentence
What is the regex pattern to identify strings that end with ‘fun’?
fun$
The dollar $ ensures that ‘fun’ is matched only if it’s at the end of the string or line
What test sentence can be used to demonstrate the regex pattern ‘fun$’?
Learning regex is fun
Expected matches: [fun] from ‘Learning regex is fun’
- What does the asterisk (*) in regex signify?
- What about ba*?
Zero or more occurrences of the preceding character
Expected Matches: [‘ba’, ‘ba’, ‘b’, ‘baaa’ from ‘bat’, ‘ball’, ‘bed’, & ‘baaah!’
Explanation: The pattern starts with the literal character ‘b’. This means it will first look for occurrences of ‘b’ in the text. Following ‘b’, we have ‘a’. Then the asterisk, which matches 0 or more occurrences of the proceeding character (‘a’ in this case)
What is the regex pattern to match a character followed by zero or more ‘a’s?
ba*
Example: Matches ‘b’, ‘ba’, ‘baa’, etc.
What is the expected match for the regex pattern ‘ba’ in the sentence ‘I saw a bat, and a ball in my bed, baaah!’?
ba, ba, b baaal
Matches from ‘bat’, ‘ball’, ‘bed’, and ‘baaah!’.
What does the plus sign (+) in regex signify?
One or more occurrences of the preceding character
Example: In the pattern ‘ba’, it matches ‘b’ followed by one or more ‘a’s.
What is the regex pattern to match a character followed by one or more ‘a’’s in the test sentence:
The battle of ba and baat
ba+
Expected Matches: ‘ba’, ‘ba’, ‘baaa’ from ‘battle’, ‘ba’, & ‘baat’
Explanation: The + matches one or more occurrences of the preceding character (‘a’ in this case)
What is the expected match for the regex pattern ‘ba’ in the sentence ‘The battle of ba and baat’?
ba, baa
Matches from ‘battle’, ‘ba’, and ‘baat’.
What does the question mark (?) in regex signify?
Zero or one occurrence of the preceding character
Example: Makes a character optional.
What is the regex pattern to match both ‘colour’ and ‘color’?
colou?r
The ‘u’ is optional in this pattern.
What is the expected match for the regex pattern ‘colou?r’ in the sentence ‘The color is nice. I like this colour’?
color, colour
Matches both variations in the text.