How to make reg exp find "h" in start of line and "o" in the end of line

Viewed 33
import re
message = 'hello' # Text
print(re.search("^ho$", message)) # None

How to make reg exp find "h" in start of line and "o" in the end of line

2 Answers

Please use

re.search("^h.*o$", message)

The dot . is a special sign, that matches every character. The asterisk * is a special sign, too, that matches as many elements of the group before.

In the combination this matches all signs between o and h.

Here you can find a list with the special characters for regular expressions.

Change the "^ho$" to "^h.o$". The "." will match any number of any type of character between the "h" and "o".

Related