regex Flashcards

1
Q

What is regular expression?

A

A search pattern in python

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does re.search do?

A

Scans string to look for pattern, returns a (customized) message of if a match is found or not.

word = input(“Enter a word: “)
pattern = ‘as’

if re.search(pattern, word):
print(“The pattern matches the word”)
else:
print(“The pattern doesn’t match the word”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you use re.search to search for more than one thing?

A

Use a list to seatch

pats = [(‘a..s’, ‘a*s’]

word = input(‘Enter a word: ‘)

for pat in pats:
if re.search(pat, word):
print(“The pattern”, pat, “matches”)

	else:
    print("The pattern", pat, "doesn't match")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

‘a..s’
What pattern would this be looking for?

A

‘a’ (two characters in between) ‘s’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

‘a*s’
What pattern would this be looking for?

A

‘a’ (followed by any number of characters) ‘s’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does re.findall() do?

A

The findall() method returns a list containing all non-overlapping matches as strings.

a = “Let’s find the word ‘regex’ using regexes!”
print(a)

re.findall(r’regex’, a)

RESULT:
[‘regex’, ‘regex’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a raw string/Why do we use it in regex?

A

Raw strings in Python are a way to specify string literals where backslashes () are treated as literal characters rather than escape characters. In a regular string, backslashes are used to escape special characters, such as newline (\n) or tab (\t). However, in raw strings, backslashes are treated as ordinary characters and not as escape characters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you differentiate between a raw string and a regular string?

A

Put an r in front

regular string
print(“This\t will do a tab space and \nthis will go on a new line\n”)

raw string
print(r”This\t won’t do a tab space and \nthis won’t go on a new line\n”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the difference in the output of re.findall vs re.search?

A

re.findall() scans the entire string and returns ALL occurances of pattern in a listr form

re.search() scans the ENTIRE string but returns only the first occurence (with specified message) or None

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does re.match() do?

A

the same thing as re.search() (Scans string to look for pattern, returns a (customized) message of if a match is found or not.) but only looks at the BEGINNING of a string for a match.

b = “123abc”

if re.match(“abc”, b):
print(“abc found with re.match”)

returns:
LITERALLY NONE

if re.search(“abc”, b):
print(“abc found with re.search”)

returns:
“abc found with re.search”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does re.split() do

A

splitting based on “!”

Splits text based on given pattern

c = “This is a sentence! re.split will split it based on the pattern! re.sub will replace the pattern”

re.split(r”!”, c)

[‘This is a sentence’,
‘ re.split will split it based on the pattern’,
‘ re.sub will replace the pattern’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does re.sub() do?

A

Replaces based on given match.

re.sub(r”!”, “;”, c)

returns:
‘This is a sentence; re.split will split it based on the pattern; re.sub will replace the pattern’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a metacharacter in regex?

A

In regular expressions (regex), a metacharacter is a character that has a special meaning or function rather than representing itself literally. Metacharacters are used to construct patterns that define search criteria. They enable you to create more flexible and powerful patterns for matching strings.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

[abc]

A

List of characters in square brackets; will match any one of them.

Specific example would match any thing that has an ‘a’, ‘b’, or ‘c’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

[^abc]

A

With a carat in the brackets, this will match any character that is NOT an ‘a’, ‘b’, or ‘c’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

”^”

A

Caret anchors a match at the start of the string

17
Q

$

A

Dollar sign anchors a match at the end of the string

18
Q

.

A

Period matches any single character.

19
Q

\

A

: Escape character to escape any special character, such as . for “.”.

would use it to search for a literal character that has another meaning

20
Q

\d

A

One digit character; matches a decimal digit character (same as [0-9]

21
Q

\D

A

One non-digit character; matches any character that is not a decimal digit (same as [^0-9]).

22
Q

\w

A

One word character; matches any alphanumeric character (letters, digits, underscores, upper and lower case) (same as [a-zA-Z0-9_]).

23
Q

\W

A

One non-word character; matches any non-alphanumeric character (anything that’s not a letter, digit, or underscore) (same as [^a-zA-Z0-9_]).

24
Q

\s

A

One whitespace character; matches whitespaces.

25
Q

\S

A

One non-whitespace character; matches any character that isn’t a whitespace.

26
Q

\b

A

Word boundary character; allows performing a “whole words only” search

27
Q

What is a quantifier?

A

Allow us to specify conditions based on how many times we want something to occur

28
Q

Quantifier:
*

A

Matches 0 or more

29
Q

Quantifier: +

A

Matches 1 or more

30
Q

Quantifier: ?

A

Matches 0 or 1

31
Q

Quantifier: {}

A

Matches whatever number is inside the brackets

32
Q

Quantifier: {2, 5}

A

Matches between 2 and 5

33
Q

Quantifier: {2, }

A

matches 2 or more

34
Q

Quantifier: {, 5}

A

Matches up to 5

35
Q

What is a greedy quanitifier?

A

A quantinfier that matches as many things as possible.

?, *, + {,}

36
Q
A