-2
l_test_word = 'in €'
print(l_test_word)

if re.match(pattern=r'[£$€]+', string=l_test_word):

    print("Match")
else:

    print("No Match")

I am trying to match if a string contains curency symbols. The code snippet above when runs returning "No Match". Unable to figure out why? I am using python 2.7.13. Thanks.

Rakesh
  • 81,458
  • 17
  • 76
  • 113

1 Answers1

-1

You're using match instead of search as Rakesh correctly said.

If you want to use match your code should be:

import re

l_test_word = 'in €'
print(l_test_word)

if re.match(pattern=r'.*?[£$€]+', string=l_test_word):

    print("Match")
else:

    print("No Match")

Note that I added .*? at the beginning of the pattern.

Cheers.

EDIT: Just to avoid a misunderstanding. Obviously this is not the best way to find a symbol like "€". re.search is better for searching, re.match is better to match a string from the beginning. The .*? is only a lazy workaround.

decadenza
  • 2,380
  • 3
  • 18
  • 31