简体   繁体   English

使用RegEx查找1个字母和2个数字

[英]Find 1 letter and 2 numbers using RegEx

I have been writing a program recently and a part of it requires me to get information form inside a string. 我最近一直在编写程序,其中一部分需要我在字符串中获取信息。 I need to find where there is 1 letter immediately followed by 2 numbers (eg S07) and I can't work out the RegEx for it. 我需要找到有1个字母的地方,紧接着是2个数字(例如S07),我无法找到它的RegEx。

def get_season(filenames):
    pattern = "^[a-zA-z]{1}[\d]{2}$"
    found = re.search(filenames[0], pattern)
    season_name = found.string
    season = season_name[1:3]
    print(season)

I know that this information is in the string but it keeps giving me "None" in response 我知道这个信息在字符串中,但它一直给我“无”作为回应

(I'm not too sure if the code section has formatted correctly, in the preview it shows as on the same line, but the indentation in my program is correct) (我不太确定代码部分是否格式正确,在预览中它显示为在同一行,但我的程序中的缩进是正确的)

You swapped the arguments to re.search() . 您将参数交换为re.search() The first argument is the pattern, not the string to match: 第一个参数是模式,而不是要匹配的字符串:

found = re.search(pattern, filenames[0])

Your pattern is also overly wide; 你的模式也过于宽泛; Az matches everything between Z (uppercase) and a (lowercase) too. Az匹配之间的一切Z (大写)和a (小写)了。 The correct pattern is: 正确的模式是:

pattern = "^[a-zA-Z]\d{2}$"

where {1} is the default, so I omitted that. 其中{1}是默认值,所以我省略了它。

If you are matching this against filenames, you probably do not want to use the start or end anchors, that would limit matches to exact strings only: 如果要将其与文件名匹配,则可能不希望使用开始或结束锚点,这会将匹配仅限于完全字符串

>>> re.search("^[a-zA-Z]\d{2}$", "S07").string
'S20'
>>> re.search("^[a-zA-Z]\d{2}$", "S07E01 - Meet the New Boss.avi") is None
True
>>> re.search("^[a-zA-Z]\d{2}$", "S07E01 - Meet the New Boss.avi") is None
True
>>> re.search("[a-zA-Z]\d{2}", "S07E01 - Meet the New Boss.avi").string
'S07E01 - Meet the New Boss.avi'

And you want to use .group() to get the matched portion, not string (which is the original input string): 并且您希望使用.group()来获取匹配的部分,而不是string (这是原始输入字符串):

>>> re.search("[a-zA-Z]\d{2}", "S07E01 - Meet the New Boss.avi").group()
'S07'

If you only wanted the numbers , you need to add a group, and pick that. 如果您只想要数字 ,则需要添加一个组,然后选择它。 You create a capturing group with parenthesis: 使用括号创建捕获组:

>>> re.search("[a-zA-Z](\d{2})", "S07E01 - Meet the New Boss.avi").group(1)
'07'

This selects the first group ( .group(1) ), which is the parenthesis around the 2 digits portion. 这将选择第一个组( .group(1) ),它是2位数部分的括号。

Your regex will catch only the string which consits only of one letter and two digits, to check whole string for multiple occurences use these: 你的正则表达式只会捕获只包含一个字母和两个数字的字符串,以检查整个字符串的多次出现使用这些:

Try this regex: 试试这个正则表达式:

[a-zA-Z]\d{2}

INPUT INPUT

asdasdasS01asfasfsa asdasdasS01asfasfsa

OUTPUT OUTPUT

S01 S01

If you want to find a word wich consists only of a letter followed by two digits use this regex: 如果你想找到一个单词,其中只包含一个字母后跟两位数,请使用此正则表达式:

\b[a-zA-Z]\d{2}\b

Only numebers capture regex: 只有数字才能捕获正则表达式:

[a-zA-Z](\d{2})

INPUT INPUT

asdasdasS01asfasfsa asdasdasS01asfasfsa

OUTPUT OUTPUT

01 01




Also swap the arguments in serach method. 也用serach方法交换参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM