-2
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

spot
  • 17
  • 4

2 Answers2

2

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.

mosc9575
  • 5,618
  • 2
  • 9
  • 32
  • 1
    Might be helpful to clarify that `.*` will match any number of any type of character between the "h" and "o" – fireshadow52 Sep 10 '22 at 16:52
  • @KellyBundy Pretty sure that was a typo, but the lack of a `$` definitely does change what the expression matches. – fireshadow52 Sep 10 '22 at 16:54
  • [Asterix](https://www.google.com/search?q=asterix&oq=aste&aqs=chrome.0.69i59j69i57j69i60j0i67j46i67l2j0i67l2j46i67j0i10i67.2702j0j7&client=ms-android-google&sourceid=chrome-mobile&ie=UTF-8)? – Kelly Bundy Sep 10 '22 at 16:57
  • @KellyBundy Sorry, that is a stupid typo. [Asterisk](https://en.wikipedia.org/wiki/Asterisk). – mosc9575 Sep 10 '22 at 16:59
0

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

Andrew
  • 1
  • 4
  • 19