Pattern Matching and Regex Flashcards

1
Q

Start With

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

End with

A

$

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

Any single character

A

.

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

Zero or one

A

?

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

zero or many

A

*

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

One or many

A

+

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

OR operator

A

|

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

[abc]

A

:set

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

[^abc]

A

:set not equal

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

[a-f]

A

:range

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

[^a-f]

A

:range not equal

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

~

A

True/False Regex:
Tests the value on the left against the regex on the right and returns true if the regex can match within the value.
Note that the regex does not have to fully match the whole value, it just has to match a part.

text ~ text → boolean
String matches regular expression, case sensitively
‘thomas’ ~ ‘t.*ma’ → t

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

~*

A

True/False Regex:
Case insensitive version of ~.
Tests the value on the left against the regex on the right and returns true if the regex can match within the value.

text ~* text → boolean
String matches regular expression, case insensitively
‘thomas’ ~* ‘T.*ma’ → t

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

.

A

Matches any character

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

\s

A

Matches “empty space” characters like space and tab

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

\S

A

Matches anything that isn’t a space

17
Q

\d

A

Matches any digit character

18
Q

\D

A

Matches any non-digit character

19
Q

\w

A

Matches any “word” character (a-z, 0-9)

20
Q

\W

A

Matches any non-word character

21
Q
A

Binds the pattern to the start of the input

22
Q

$

A

Binds the pattern to the end of the input

23
Q

( )

A

Group items in a single logical item to be made available for further processing.

Example:
c(a|o)%.
Matches: can, corn, cop

24
Q

*

A

Matches 0-N repetitions of the character preceding it

25
Q

+

A

Matches 1-N repetitions of the character preceding it

26
Q

{N}

A

Matches N repetitions of the character preceding it

27
Q

^A+

A

Would match strings that start with one-or-more ‘A’ characters

28
Q

\d+

A

Would match any combination of one-or-more digits in order

29
Q

^\S

A

Would match any string that did not start with white space

30
Q

!~

A

text !~ text → boolean
String does not match regular expression, case sensitively
‘thomas’ !~ ‘t.*max’ → t

31
Q

!~*

A

text !~* text → boolean
String does not match regular expression, case insensitively
‘thomas’ !~* ‘T.*ma’ → f

32
Q

regex_replace()

A

Provides substitution of new text for substrings that match POSIX regular expression patterns. It has the syntax regexp_replace(source, pattern, replacement [, flags ]).

The source string is returned unchanged if there is no match to the pattern. If there is a match, the source string is returned with the replacement string substituted for the matching substring.

Examples:
regexp_replace(‘foobarbaz’, ‘b..’, ‘X’)
fooXbaz
regexp_replace(‘foobarbaz’, ‘b..’, ‘X’, ‘g’)
fooXX
regexp_replace(‘foobarbaz’, ‘b(..)’, ‘X\1Y’, ‘g’)
fooXarYXazY

33
Q

substring()

A

The substring function with two parameters, substring(string from pattern), provides extraction of a substring that matches a POSIX regular expression pattern.

It returns null if there is no match, otherwise the first portion of the text that matched the pattern.

But if the pattern contains any parentheses, the portion of the text that matched the first parenthesized subexpression (the one whose left parenthesis comes first) is returned.

You can put parentheses around the whole expression if you want to use parentheses within it without triggering this exception. If you need parentheses in the pattern before the subexpression you want to extract, see the non-capturing parentheses described below.

Examples:
substring(‘foobar’ from ‘o.b’) oob
substring(‘foobar’ from ‘o(.)b’) o

34
Q

%

A

Any string (using LIKE and SIMILAR TO)

Example:
c%
Matches:
chart, articulation, crate