捕获组与量词Regexp匹配 [英] Capture groups match with quantifier Regexp

查看:62
本文介绍了捕获组与量词Regexp匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是regex世界中的新手,我需要捕获一些不同类型的字符串.

I am newbie in regex world, I need to capture some different types of strings.

通过这种方式,请提出更愚蠢的方式来捕获此类字符串. n =任何正数(不相同)

By the way please suggest more elagant way to capture such strings. n = any positive number(not the same)

|n||0||0||0||0|
|n||n||0||0||0|
|n||n||n||0||0|
|n||n||n||n||0|
|n||n||n||n||n|

我试图使用这样的正则表达式来捕获字符串的第一和第二类型

I have tried to use such regular expression for capturing first and secodn types of strings

^\|([1-9]+)\|(?:([1-9]+)\|){4}|(?:(0)\|){4}$

零应视为单独的字符, 我需要捕获每个数字或零

Zero should be treated as separate char, I need to capture each number or zero

现在的问题是,它仅捕获第一个匹配的字符和最后一个匹配的字符

The problem now that it only captures first matched character and last one

但不能捕获其他数字

请帮助使用此正则表达式,如果有人提供更多卑​​鄙的方式(最后,我必须编写4个ors语句来捕获我的字符串类型),那就太好了

Please help with this regular expression and it would be great if someone provides more elagant way ( at the end, I have to write 4 ors statements to capture my string types)

谢谢

推荐答案

我不确定是否足以满足您需求:

I'm not sure wheter it is sufficient for you or not:

\|(?:(0)|([0-9]+))\|

https://regex101.com/r/fX5xI4/2

现在,您必须将您的匹配项分成x个元素组,其中x是列数.我想那应该没事.

Now u have to split your matches into groups of x elements where x is number of colums. I suppose that should be just fine.

这篇关于捕获组与量词Regexp匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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