0

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:
5
  • Yes, looks like you need a regex. The re module has everything you need. Commented May 24, 2013 at 12:32
  • Using the leading zero as a marker wouldn't work as that would erroneously match substrings that are preceded by a zero in the target number. For your example e.g. 8352. Is the number you're after always in that block preceded by multiple zeros and the '-'? Commented May 24, 2013 at 12:33
  • it is always 0 to n 0's and then a -. I think it is a safe bet that it will be multiple 0's. Commented May 24, 2013 at 12:36
  • you are searching for '-', then only 0s then your string ? Commented May 24, 2013 at 12:52
  • Are there always 10 digits between - and @? Commented May 24, 2013 at 12:53

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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@'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.