简体   繁体   English

正则表达式仅匹配带有或不带有特定格式数字的大写字符串

[英]Regex to match only uppercase strings with or without numbers in specific format

I have the following list of strings:我有以下字符串列表:

list1 = ['KVC Company','this is a sample', 'TEL 555-555-5555', 'DATE: 05/12/2021', 
'50 KG LOADER', 'COMPUTER SCIENCE', '13445556 AAA', 'MONDAY', 'AMT: 60', 'plenty', 
'data sources','USA 00000','EXM: J. Smith', 'X', 'FH']

I'm using the following regex to filter the list:我正在使用以下正则表达式来过滤列表:

r'\b[A-Z]{3,}(?:\s+[A-Z]{3,})*\b.*$'

This regex matches all the strings that begin with uppercase letters but doesn't consider the rest of the letters.此正则表达式匹配所有以大写字母开头的字符串,但不考虑字母的 rest。

I'm getting the output as follows,我得到 output 如下,

output = ['KVC Company', 'TEL 555-555-5555', 'DATE: 05/12/2021', 'LOADER', 
'COMPUTER SCIENCE', 'AAA','MONDAY', 'AMT: 60', 'USA 00000','EXM: J. Smith']

My desired output is as shown below,我想要的output如下图,

['50 KG LOADER', 'COMPUTER SCIENCE', 'MONDAY' ]

which contains only strings that are completely uppercase and doesn't have any symbols and have only numbers not more than 2 digits .它只包含完全大写的字符串,没有任何符号只有不超过 2 位的数字

How can I filter out all other strings and return only string which are completely uppercase having only 2 digit numbers and doesn't have any symbols like :,/,- and lower case letters anywhere in the string.我怎样才能过滤掉所有其他字符串并只返回完全大写的字符串,只有 2 位数字,并且在字符串中的任何地方都没有任何符号,如:,/,-和小写字母。

You may use this regex in Python:您可以在 Python 中使用此正则表达式:

^(?!.*\d{3})(?=.*[A-Z]{3})[A-Z\d ]+$

RegEx Demo正则表达式演示

RegEx Details:正则表达式详细信息:

  • ^ : Start ^ : 开始
  • (?..*\d{3}) : Negative lookahead to assert that we don't have 3 digits anywhere (?..*\d{3}) :否定前瞻断言我们在任何地方都没有 3 位数字
  • (?=.*[AZ]{3}) : Positive lookahead to assert that we have at least 3 consecutive uppercase letters, somewhere (?=.*[AZ]{3}) :肯定的前瞻性断言我们在某处至少有 3 个连续的大写字母
  • [AZ\d ]+ : Match 1+ of uppercase letter or digit or space [AZ\d ]+ : 匹配大写字母或数字或空格的 1+
  • $ : End $ : 结束

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

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