一个更好的RE? [英] A better RE?

查看:66
本文介绍了一个更好的RE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个与21MAR06 31APR06 1236,

匹配的字符串,其中最后一部分是天数(1-7),即它可以包含

数字1-7,按顺序,每个只有一个,至少一个

数字。我想把它当成三组。我在考虑


r"(\\\\ [AZ] \\\)(\\\\ [[[[[[[[[[[[[[[[[[[[dd d)(1?2?3?4?5?6?7?)


但是即使第三组是空的也会匹配,

对吗?

这个人有没有好的而不是过于复杂的RE?


P.S.我知道现在你有两个问题回复......

I want an re that matches strings like "21MAR06 31APR06 1236",
where the last part is day numbers (1-7), i.e it can contain
the numbers 1-7, in order, only one of each, and at least one
digit. I want it as three groups. I was thinking of

r"(\d\d[A-Z]\d\d) (\d\d[A-Z]\d\d) (1?2?3?4?5?6?7?)"

but that will match even if the third group is empty,
right? Does anyone have good and not overly complex RE for
this?

P.S. I know the "now you have two problems reply..."

推荐答案

Magnus Lycka写道:
Magnus Lycka wrote:
我想要一个与21MAR06 31APR06 1236等字符串匹配的重新编号,其中最后一部分是日期编号(1-7),即它可以包含数字1-7,in订单,每个只有一个,至少有一个数字。我想把它当成三组。我想的是

r"(\d\ [AZ] \\\)(\\\\ [[[[[[[[[[[[[[[[[[[dd ?2?3?4?5?6?7?)

但即使第三组是空的,它也会匹配,对吧?有没有人对这个有好的而不是过于复杂的RE?
I want an re that matches strings like "21MAR06 31APR06 1236",
where the last part is day numbers (1-7), i.e it can contain
the numbers 1-7, in order, only one of each, and at least one
digit. I want it as three groups. I was thinking of

r"(\d\d[A-Z]\d\d) (\d\d[A-Z]\d\d) (1?2?3?4?5?6?7?)"

but that will match even if the third group is empty,
right? Does anyone have good and not overly complex RE for
this?




怎么样(未经测试)


r" ;(\d \ [AZ] {3} \\\)(\\\\ [AZ] {3} \\\)(?= [1234567])(1 ?2?3?4?5?6?7?)


其中{3}表示需要三份以前的RE部分,并且

(?= [1234567])表示要求1-7中的至少一个,但如果匹配,则不要向前移动




< ; / F>



how about (untested)

r"(\d\d[A-Z]{3}\d\d) (\d\d[A-Z]{3}\d\d) (?=[1234567])(1?2?3?4?5?6?7?)"

where {3} means require three copies of the previous RE part, and
(?=[1234567]) means require at least one of 1-7, but don''t move
forward if it matches.

</F>


Magnus Lycka写道:
Magnus Lycka wrote:
我想要一个与21MAR06 31APR06 1236等字符串匹配的重新编号,
其中最后一部分是天数(1-7),即它可以包含数字1-7,按顺序,每个只有一个,至少有一个数字。我想把它当成三组。我想的是

r"(\d\ [AZ] \\\)(\\\\ [[[[[[[[[[[[[[[[[[[dd ?2?3?4?5?6?7?)

但即使第三组是空的,它也会匹配,对吧?对于
这个有没有好的而不是过于复杂的RE?

P.S。我知道现在你有两个问题回复......
I want an re that matches strings like "21MAR06 31APR06 1236",
where the last part is day numbers (1-7), i.e it can contain
the numbers 1-7, in order, only one of each, and at least one
digit. I want it as three groups. I was thinking of

r"(\d\d[A-Z]\d\d) (\d\d[A-Z]\d\d) (1?2?3?4?5?6?7?)"

but that will match even if the third group is empty,
right? Does anyone have good and not overly complex RE for
this?

P.S. I know the "now you have two problems reply..."


txt = 21MAR06 31APR06 1236
m =''(?:JAN | FEB | MAR | APR | MAI | JUN | JUL | AUG | SEP | OCT | NOV | DE Z)''
#非捕获组(:?)

p = re.compile(r"(\\\ %% s \\\\))(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ (?= [1234567])(1?2?3?4?5?6?7?)%(m,m))

p.match(txt).group(1 )
''21MAR06''

p.match(txt).group(2)
''31APR06''

p.match( txt).group(3)
txt = "21MAR06 31APR06 1236" m = ''(?:JAN|FEB|MAR|APR|MAI|JUN|JUL|AUG|SEP|OCT|NOV|DE Z)'' # non capturing group (:?)
p = re.compile(r"(\d\d%s\d\d) (\d\d%s\d\d) (?=[1234567])(1?2?3?4?5?6?7?)" % (m,m))
p.match(txt).group(1) ''21MAR06''
p.match(txt).group(2) ''31APR06''
p.match(txt).group(3)



1236


1236


Magnus Lycka写道:
Magnus Lycka wrote:
我想要一个匹配像21MAR06 31APR06 1236这样的字符串的re,其中最后一部分是日期编号(1-7),即它可以包含
数字1-7,按顺序,每个只有一个,至少有一个数字。我想把它当成三组。我想的是

r"(\d\ [AZ] \\\)(\\\\ [[[[[[[[[[[[[[[[[[[dd ?2?3?4?5?6?7?)

但即使第三组是空的,它也会匹配,对吧?对于
这个人有没有好的而不是过于复杂的RE?


最简单:
I want an re that matches strings like "21MAR06 31APR06 1236",
where the last part is day numbers (1-7), i.e it can contain
the numbers 1-7, in order, only one of each, and at least one
digit. I want it as three groups. I was thinking of

r"(\d\d[A-Z]\d\d) (\d\d[A-Z]\d\d) (1?2?3?4?5?6?7?)"

but that will match even if the third group is empty,
right? Does anyone have good and not overly complex RE for
this?
Simplest:
exp = r"(\d {2} [AZ] {3} \d {2})(\d {2} [AZ] {3} \d {2})(\d +)"
re.match (exp,s).groups()
exp = r"(\d{2}[A-Z]{3}\d{2}) (\d{2}[A-Z]{3}\d{2}) (\d+)"
re.match(exp, s).groups()



(''21MAR06'',''31APR06'',''1236'')br />

但这可能会给你误报,取决于真实数据。


如果你想尽可能严格,这会变得有点有毛的。

PS我知道现在你有两个问题回复......


(''21MAR06'', ''31APR06'', ''1236'')

but this could give you false positive, depending on the real data.

If you want to be as strict as possible, this becomes a little bit hairy.
P.S. I know the "now you have two problems reply..."




! - )


- < br $> b $ b bruno desthuilliers

python -c" print''@''。join([''。''。join([w [:: - 1] for w在p.split(''。'')])中'/ o **** @ xiludom.gro'中的
p。分裂(''@'')])"



!-)

--
bruno desthuilliers
python -c "print ''@''.join([''.''.join([w[::-1] for w in p.split(''.'')]) for
p in ''o****@xiludom.gro''.split(''@'')])"


这篇关于一个更好的RE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆