2

I have an section id A00-A09. Anything like A01, A01.01, A02 till A09.09 should be classified under this section id. How can i do this in Python? At the moment I can only match string with exact character.

2
  • Check the re module, regex Commented Apr 2, 2014 at 9:17
  • @sshashank124 You mean the re module? Commented Apr 2, 2014 at 9:18

4 Answers 4

1

You can use [] with re module:

re.findall('A0[0-9].0[0-9]|A0[0-9]','A01')

output:

['A01']

Non occurance:

re.findall('A0[0-9].0[0-9]|A0[0-9]','A11')

output:

[]
Sign up to request clarification or add additional context in comments.

Comments

1

Use re.match() to check this. here is an example:

import re

section_id = "A01.09"
if re.match("^A0[0-9](\.0[0-9])?$", section_id):
    print "yes"

Here the regex means A0X is mandatory, and .0X is optional. X is from 0-9.

Comments

0

Cut the section id and compare:

sid = "A00-A09"

def under_sid(ssid, sid):
    sid_start, sid_end = sid.split("-")
    return ssid[:3] >= sid_start and ssid[:3] <= sid_end

for i in ["A01", "A01.01", "A02", "A09.09"]:
    assert under_sid(i, sid)

for i in ["B01", "A22.01", "A93", "A19.09"]:
    assert not under_sid(i, sid)

Comments

0

You can do partial matches using startswith() and endswith(). Assuming the full id is always in a X12.Y34 - each part is a letter and two numbers, separated by . or - (or any character):

>>> id = 'A03.A07'
>>> section_id = id[:3]
>>> section_id 
'A03'
>>> id.startswith('A03')
True
>>> id.startswith('A07')
False  # so won't match with the subsection.
>>> sub_section_id = id[-3:]
>>> sub_section_id 
'A07'

And you can convert it to uppercase if the input can sometimes be lowercase.

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.