Tutorial 5: Regular Expressions in Python — Using regex characters ‘ ^ $ \b \B’

Gaurav Patil
3 min readSep 29, 2021

--

Photo by Ilya Pavlov on Unsplash

Missed previous tutorial? — Click here

You can directly navigate to the topic of your interest by clicking on topic name given below :

1. Caret symbol ‘ ^ ’

2. Dollar symbol ‘ $ ’

3. Combination of ‘ ^ ’ and ‘ $ ’

4. word boundary — ‘ \b ’

5.not word boundary — ‘ \B ’

1. Caret symbol ‘ ^ ’

a) This matches with the position not character. It is used to match the start of the string.

b) It is used to match the beginning of each line when text has multiple lines with newline if the multiline flag is enabled.

More about flags — check Tutorial 7

c) It is also used with [ ] to match a negated set. For example, [^abcd] will match any character except ‘a’, ‘b’, ‘c’ or ‘d’.

2. Dollar symbol ‘ $ ’

a) This matches with the position not character. It is used to match the end of the string.

b) It is used to match the end of last line when text has multiple lines with newline if the multiline flag is enabled.

More about flags — check Tutorial 7

3. Combination of ‘ ^ ’ and ‘ $ ’

This matches with start and end i.e. entire string when correct regex code is written between ‘^’ and ‘$’ .

4. word boundary — ‘ \b ’

Word boundary is indicated by whitespace or a non-alphanumeric character.

5. not word boundary — ‘ \B ’

Not word boundary is indicated by a alphanumeric character.

Combination of \b and \B:

Do not worry about remembering all these things just understand the meaning of word (or not word) boundary and whenever you have problem try experimenting with few examples. Even I do the same things.

This is quite a task you completed today. Great work !!!

Take a deep breathe and move to the next tutorial.

Happy learning !!!

--

--

Gaurav Patil
Gaurav Patil

Written by Gaurav Patil

Machine Learning Engineer drawing insights from mathematics.

Responses (1)