简体   繁体   English

将特定模式与正则表达式匹配

[英]Match specific pattern with regular expression

I've to make a regex to match exactly this kind of pattern here an example 我要制作一个正则表达式来匹配这种模式这里的一个例子

JK+6.00,PP*2,ZZ,GROUPO JK + 6.00,PP * 2,ZZ,GROUPO

having a match for every group like 每个小组都有匹配

Match 1 比赛1

  • JK JK
  • + +
  • 6.00 6.00

Match 2 比赛2

  • PP PP
  • * *
  • 2 2

Match 3 比赛3

  • ZZ Z Z

Match 4 比赛4

  • GROUPO GROUPO

So comma separated blocks of (2 to 12 all capitals letters) [optional (+ or *) and a (positive number 0[.0[0]]) 所以逗号分隔的块(2到12个所有大写字母)[可选(+或*)和a(正数0 [.0 [0]])

This block successfully parse the pattern 该块成功解析模式

(?P<block>(?P<subject>[A-Z]{2,12})(?:(?P<operation>\*|\+)(?P<value>\d+(?:.?\d{1,2})?))?)

we have the subject group 我们有主题组

(?P<subject>[A-Z]{2,12})

The value 价值

(?P<value>\d+(?:.?\d{1,2})?)

All the optional operation section (value within) 所有可选操作部分(值内)

(?:(?P<operation>\*|\+)(?P<value>\d+(?:.?\d{1,2})?))?

But the regex must fail if the string doesn't match EXACTLY the pattern and that's the problem 但是如果字符串与模式不完全匹配,则正则表达式必须失败,这就是问题所在

I tried this but doesn't work 我尝试了这个但是没有用

^(?P<block>(?P<subject>[A-Z]{2,12})(?:(?P<operation>\*|\+)(?P<value>\d+(?:.?\d{1,2})?))?)(?:,(?P=block))*$

Any suggestion? 有什么建议吗?

PS. PS。 I use Python re 我用Python重新编写

I'd personally go for a 2 step solution, first check that the whole string fits to your pattern, then extract the groups you want. 我个人会采用两步解决方案,首先检查整个字符串是否适合您的模式,然后提取您想要的组。

For the overall check you might want to use ^(?:[AZ]{2,12}(?:[*+]\\d+(?:\\.\\d{1,2})?)?(?:,|$))*$ as a pattern, which contains basically your pattern, the (?:,|$) to match the delimiters and anchors. 对于整体检查,你可能想要使用^(?:[AZ]{2,12}(?:[*+]\\d+(?:\\.\\d{1,2})?)?(?:,|$))*$作为模式,基本上包含你的模式, (?:,|$)匹配分隔符和锚点。

I have also adjusted your pattern a bit, to (?P<block>(?P<subject>[AZ]{2,12})(?:(?P<operation>[*+])(?P<value>\\d+(?:\\.\\d{1,2})?))?) . 我也调整了你的模式, (?P<block>(?P<subject>[AZ]{2,12})(?:(?P<operation>[*+])(?P<value>\\d+(?:\\.\\d{1,2})?))?) I have replaced (?:\\*|\\+) with [+*] in your operation pattern and \\. 我已经在你的操作模式和\\.中用[+*]替换了(?:\\*|\\+) \\. with .? .? in your value pattern. 在你的价值模式。

A (very basic) python implementation could look like 一个(非常基本的)python实现可能看起来像

import re
str='JK+6.00,PP*2,ZZ,GROUPO'
full_pattern=r'^(?:[A-Z]{2,12}(?:[*+]\d+(?:\.\d{1,2})?)?(?:,|$))*$'
extract_pattern=r'(?P<block>(?P<subject>[A-Z]{2,12})(?:(?P<operation>[*+])(?P<value>\d+(?:\.\d{1,2})?))?)'
if re.fullmatch(full_pattern, str):
    for match in re.finditer(extract_pattern, str):
        print(match.groups())

http://ideone.com/kMl9qu http://ideone.com/kMl9qu

I'm guessing this is the pattern you were looking for: 我猜这是你要找的模式:

(2 different letter)+(time stamp),(2 of the same letter)*(1 number),(2 of the same letter),(a string) (2个不同的字母)+(时间戳),(2个相同的字母)*(1个数字),(2个相同的字母),(一个字符串)

If thats the case, this regex would do the trick: 如果是这样的话,这个正则表达式可以解决这个问题:

^(\\w{2}\\+\\d{1,2}\\.\\d{2}),((\\w)\\3\\*\\d),((\\w)\\5),(\\w+)$

Demo: https://regex101.com/r/8B3C6e/2 演示: https//regex101.com/r/8B3C6e/2

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

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