Partial string matching in python

2020-04-16 06:33发布

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.

4条回答
家丑人穷心不美
2楼-- · 2020-04-16 07:15

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:

[]
查看更多
不美不萌又怎样
3楼-- · 2020-04-16 07:28

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.

查看更多
forever°为你锁心
4楼-- · 2020-04-16 07:33

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)
查看更多
Explosion°爆炸
5楼-- · 2020-04-16 07:35

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.

查看更多
登录 后发表回答