RegEx和匹配代码从右到左

时间:2022-08-22 12:11:34

Stuggling a little bit with the RegEx, I've got 4 codes in a string

使用RegEx进行一些处理,我在一个字符串中有4个代码

CODE4:CODE3:CODE2:CODE1

each code is optional apart from CODE1

除CODE1外,每个代码都是可选的

So I could have ab:bc:de:fg

所以我可以拥有ab:bc:de:fg

or

bc::fg

of

ab:::fg

In each case of the above CODE1 = fg dnd for the dear life of me I can't work out the RegEX

在上述CODE1 = fg dnd的每一种情况下,对于我的亲爱的生活,我无法解决RegEX问题

Would be easy to do as a standard string parse, but unforunatly because of buisness objects in needs to be done via regex :-( and return via a vb.net RegEX.matche,groups("Code1") fg (I hope that makes sense)

作为一个标准的字符串解析很容易做,但由于需要通过正则表达式完成商业对象,因此需要通过vge.net RegEX.matche,group(“Code1”)返回fg(我希望这样做)感)

Thanks in advance for any help

在此先感谢您的帮助

Ended up with a bit of RegEx that does the job, bit messy but it works

结束了一些RegEx来做这项工作,有点凌乱,但它的工作原理

(^(?<code1>[\w]*)$)|(^(?<code2>[\w]*):(?<code1>[\w]*)$)|(^(?<code3>[\w]*):(?<code2>[\w]*):(?<code1>[\w]*)$)|(^(?<code4>[\w]*):(?<code3>[\w]*):(?<code2>[\w]*):(?<code1>[\w]*)$)

Ta all

3 个解决方案

#1


There's no need to use a regular expression here.

这里不需要使用正则表达式。

I don't know what language you're using, but split the string on ':' and you'll have an array of codes.

我不知道你正在使用什么语言,但是将字符串拆分为':',你将拥有一系列代码。

If you really just want to validate whether a string is valid for this then

如果你真的只想验证字符串是否对此有效

/(\w*:){0,3}\w+/

matches your description and the few examples you've given.

符合您的描述和您给出的几个示例。

#2


I'm not sure why you have to match the codes right to left. Simply use a regular expression to pick apart the string:

我不确定为什么你必须从右到左匹配代码。只需使用正则表达式来分离字符串:

/(.*):(.*):(.*):(.+)/

and then you have CODE1 in $4, CODE2 in $3, CODE3 in $2, CODE4 in $1.

然后你有4美元的CODE1,3美元的CODE2,2美元的CODE3,1美元的CODE4。

#3


(CODE1)?:(CODE2)?:(CODE3)?:CODE4 would work - if the leading : don't matter. Otherwise, if you can't have leading colons, enumerate:

(CODE1)?:( CODE2)?:( CODE3)?:CODE4会起作用 - 如果领先:无所谓。否则,如果您不能拥有前导冒号,请枚举:

(CODE1:(CODE2)?:(CODE3)?:|CODE2:(CODE3)?:|CODE3)?CODE4

There's nothing special about the fact that the right-most part is mandatory, and the left-most parts aren't.

没有什么特别的事实,最右边的部分是强制性的,而最左边的部分则不是。

#1


There's no need to use a regular expression here.

这里不需要使用正则表达式。

I don't know what language you're using, but split the string on ':' and you'll have an array of codes.

我不知道你正在使用什么语言,但是将字符串拆分为':',你将拥有一系列代码。

If you really just want to validate whether a string is valid for this then

如果你真的只想验证字符串是否对此有效

/(\w*:){0,3}\w+/

matches your description and the few examples you've given.

符合您的描述和您给出的几个示例。

#2


I'm not sure why you have to match the codes right to left. Simply use a regular expression to pick apart the string:

我不确定为什么你必须从右到左匹配代码。只需使用正则表达式来分离字符串:

/(.*):(.*):(.*):(.+)/

and then you have CODE1 in $4, CODE2 in $3, CODE3 in $2, CODE4 in $1.

然后你有4美元的CODE1,3美元的CODE2,2美元的CODE3,1美元的CODE4。

#3


(CODE1)?:(CODE2)?:(CODE3)?:CODE4 would work - if the leading : don't matter. Otherwise, if you can't have leading colons, enumerate:

(CODE1)?:( CODE2)?:( CODE3)?:CODE4会起作用 - 如果领先:无所谓。否则,如果您不能拥有前导冒号,请枚举:

(CODE1:(CODE2)?:(CODE3)?:|CODE2:(CODE3)?:|CODE3)?CODE4

There's nothing special about the fact that the right-most part is mandatory, and the left-most parts aren't.

没有什么特别的事实,最右边的部分是强制性的,而最左边的部分则不是。