简体   繁体   English

匹配逗号 - 正则表达式

[英]Match up to the comma - Regex

I have created a Regex Pattern (?<=[TCC|TCC_BHPB]\s\d{3,4})[-_\s]\d{1,2}[,] This Pattern match just:我创建了一个正则表达式模式(?<=[TCC|TCC_BHPB]\s\d{3,4})[-_\s]\d{1,2}[,]这个模式匹配只是:

TCC 6005_5,

What should I change to the end to match these both strings:我应该更改到最后以匹配这两个字符串:

TCC 6005-5 ,
TCC 6005_5,

You can add a non-greedy wildcard to your expression ( .*? ):您可以在表达式中添加非贪婪通配符 ( .*? ):

(?<=(?:TCC|TCC_BHPB)\s\d{3,4})[-_\s]\d{1,2}.*?[,]
                                           ^^^

This will now also match any characters between the last digit and the comma.这现在也将匹配最后一位数字和逗号之间的任何字符。

As has been pointed out in the comments, [TCC|TCC_BHPB] is a character class rather than a literal match, so I've changed this to (?:TCC|TCC_BHPB) which is presumably what your intention was.正如评论中所指出的, [TCC|TCC_BHPB]是一个字符 class 而不是文字匹配,所以我将其更改为(?:TCC|TCC_BHPB)这大概是您的意图。

Try it online 在线尝试

This part of the pattern [TCC|TCC_BHPB] is a character class that matches one of the listed characters.模式[TCC|TCC_BHPB]的这一部分是一个字符 class 匹配列出的字符之一。 It might also be written for example as [|_TCBHP]例如,它也可以写成[|_TCBHP]

To "match" both strings, you can match all parts instead of using a positive lookbehind.要“匹配”两个字符串,您可以匹配所有部分,而不是使用正向的后视。

\bTCC(?:_BHPB)?\s\d{3,4}[-_\s]\d{1,2}\s?,

See a regex demo查看正则表达式演示

  • \bTCC A word boundary to prevent a partial match, then match TCC \bTCC防止部分匹配的单词边界,然后匹配TCC
  • (?:_BHPB)?\s\d{3,4} Optionally match _BHPB , match a whitespace char and 3-4 digits (Use [0-9] to match a digit 0-9) (?:_BHPB)?\s\d{3,4}可选匹配_BHPB ,匹配空格字符和 3-4 位数字(使用[0-9]匹配数字 0-9)
  • [-_\s]\d{1,2} Match one of - _ or a whitespace char [-_\s]\d{1,2}匹配- _或空白字符之一
  • \s?, Match an optional space and , \s?,匹配一个可选的空格和,

Note that \s can also match a newline.请注意, \s也可以匹配换行符。


Using the lookbehind:使用后视:

(?<=TCC(?:_BHPB)?\s\d{3,4})[-_\s]\d{1,2}\s?,

Regex demo正则表达式演示

Or if you want to match 1 or more spaces except a newline或者,如果您想匹配除换行符之外的 1 个或多个空格

\bTCC(?:_BHPB)?[\p{Zs}\t][0-9]{3,4}[-_\p{Zs}\t][0-9]{1,2}[\p{Zs}\t]*,

Regex demo正则表达式演示

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

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