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

Question:

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

Asked By: spot

||

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.

Answered By: mosc9575

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

Answered By: Andrew
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.