user3446735
user3446735

Reputation: 125

Partial string matching in python

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.

Upvotes: 2

Views: 17194

Answers (4)

Sabuj Hassan
Sabuj Hassan

Reputation: 39355

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.

Upvotes: 1

venpa
venpa

Reputation: 4318

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:

[]

Upvotes: 1

aneroid
aneroid

Reputation: 15962

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.

Upvotes: 0

user1907906
user1907906

Reputation:

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)

Upvotes: 0

Related Questions