简体   繁体   English

Python正则表达式检查长字符串匹配模式

[英]Python Regex to Check Long List of Strings Match a Pattern

I would like to test a string: 我想测试一个字符串:

BASE[AZ] + SINGLE CHAR {F,G,H,J,K,M,N,Q,U,V,X,Z} + SINGLE NUMERIC [0-9] BASE [AZ] +单字符{F,G,H,J,K,M,N,Q,U,V,X,Z} +单数值[0-9]

The 'BASE' is at least one character long. “ BASE”的长度至少为一个字符。

eg. 例如。 'ESZ6' -> True, 'ESSP6' -> False 'ESZ6'-> True,'ESSP6'-> False

I appreciate I can do: 我能做到:

import re
prog = re.compile('[A-Z]+[FGHJKMNQUVXZ]{1}[\d]{1}')

...

if prog.search(string):
    print('This matched...')

I then wanted to use: 然后,我想使用:

matches = [i for i in items if prog.search(item)]

Is this the optimal way of implementing this? 这是实现此目标的最佳方法吗?

This depends on what you mean by, 'test a string'. 这取决于“测试字符串”的含义。 Do you want to check if the entire string matches your pattern, or if the pattern just happens to occur in your string, eg 'ESZ6' vs. "I've got an ESZ6 burning a hole in my pocket'. Can other characters abut your target, eg. '123ESZ6ARE'? 您是否要检查整个字符串是否与您的模式匹配,或者该模式是否恰好出现在您的字符串中,例如“ ESZ6”与“我的ESZ6在口袋里烧了一个洞”。其他字符可以邻接吗?您的目标,例如“ 123ESZ6ARE”?

Assuming we're just testing individual tokens like 'ESZ6' and 'ESSP6', then here are some code ideas: 假设我们只是测试单个令牌,例如“ ESZ6”和“ ESSP6”,那么这里有一些代码提示:

import re

items = ('ESZ6', 'ESSP6')

prog = re.compile(r"[A-Z]+[FGHJKMNQUVXZ]\d$")

matches = [item for item in items if prog.match(item)]

Use .match() instead of .search() unless you want an unanchored search. 除非要进行无锚定搜索,否则请使用.match()而不是.search()。 Drop the final '$' if you want the end unanchored. 如果您不希望结尾结尾,请删除最后的'$'。 (If using Python 3.4 or later, and want an anchored search, you can probably drop the '$' and use .fullmatch instead of .match) (如果使用Python 3.4或更高版本,并且希望进行锚定搜索,则可以删除'$'并使用.fullmatch而不是.match)

Pattern match operators only match one character sans repetition operators so you don't need the {1} indications. 模式匹配运算符仅匹配一个字符,无重复运算符,因此您不需要{1}指示。 Use raw strings, r"\\d", when dealing with patterns to keep Python from messing with your back slashes. 处理模式时,请使用原始字符串r“ \\ d”,以防止Python弄乱您的反斜线。

Your description and your examples don't match exactly so I'm making some assumptions here. 您的描述和示例不完全匹配,因此我在这里进行一些假设。

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

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