Reputation: 5974
I am searching a string [email protected]
for 108352
, this should return true. However if I search for a substring of this it should not return true. Eg if I searched for 08352
, this is missing the 1 so that would be false. How should I accomplish this?
I was searching like this:
for item in parse:
if element in item:
where element
is 08352
and parse is several strings in a list. This is returning the positives I don't want.
perhaps I should look for a pattern? After each string I search for is a @
I notice, also before each one is a 0
. So perhaps a regex? And somehow incorporate it into my for
and if
?
Edit: what if I prepend "00" to the search string and add @ at the end? Like:
if "00"+access_point_id+"@" in item:
Upvotes: 0
Views: 160
Reputation: 25207
If you are looking for a string of 10 digits, you can add padding to the string you search for:
>>> '{:0>10}'.format('08352')
'0000008352'
>>> '-{:0>10}@'.format('08352')
'-0000008352@'
Upvotes: 1
Reputation: 33092
A simple infix search should suffice:
found = ("0%s@" % element) in item
a regular expression like -0+(\d+)@
is safer, though:
m = re.search(r"-0+(\d+)@", item)
found = m and m.group(1) == element
Upvotes: 1