Amit Ron
Amit Ron

Reputation: 41

Python - Searching a hyphen in a given string

I tried using the following line to print true if a hyphen is found in a string:

print (re.match('-', 'p-abcd-abcd'))

Instead of 'true', 'None' is printed.

Upvotes: 2

Views: 10774

Answers (1)

Kasravnd
Kasravnd

Reputation: 107337

re.match will match the pattern from the start of the string. If you want to search a pattern within a string You need re.search():

re.search(r'-', 'p-abcd-abcd')

But if you just want to check the membership of a character in a string,you can simply use in operand :

if '-' in 'p-abcd-abcd'

Upvotes: 3

Related Questions