2

I want to be able to capture a repeating group in a single line. I have done my work as shown below;

(((?:\s*^>\s*[0-9]+\s*,\s*[0-9]+\s*,\s*[a-zA-Z]+\s*(,\s*[a-zA-Z]+\s*)*;$\s*)|(?:\s*^>\s*[0-9]+\s*,\s*[0-9]+\s*,\s*[a-zA-Z]+\s*,\s*[0-9]+\s*(,\s*[\-]?[0-9]+\s*)*;$\s*))+)

Regular expression visualization

Edit live on Debuggex

It captures > 9, 2, door, open; and > 3, 3, door,1, 1; individually fine. However, I'd like to capture > 9, 2, door, close; > 1, 9, door, close; > 3, 3, door, 1, 1; as well. I enclosed my group by using parenthesis with + quantifier at the end, but it does not capture repeating pattern correctly. Could you show me where I did wrong?

EDITED

I made the regex somewhat shorter as follows;

(((\s*>\s*\d+\s*,\s*\d+\s*,\s*\w+\s*(,\s*\w+\s*)*;\s*)|(\s*>\s*\d+\s*,\s*\d+\s*,\s*\w+\s*,\s*\d+\s*(,\s*[\-]?\d+\s*)*;\s*))+)

Regular expression visualization

4

2 回答 2

2

If you mean to write

> 9, 2, door, close; > 1, 9, door, close; > 3, 3, door, 1, 1;

in one line so you got to fix your regex by removing the ^ and $ totally so this will match

(((?:\s*>\s*[0-9]+\s*,\s*[0-9]+\s*,\s*[a-zA-Z]+\s*(,\s*[a-zA-Z]+\s*)*;\s*)|(?:\s*>\s*[0-9]+\s*,\s*[0-9]+\s*,\s*[a-zA-Z]+\s*,\s*[0-9]+\s*(,\s*[\-]?[0-9]+\s*)*;\s*))+)

In case you mean

> 9, 2, door, close;
> 1, 9, door, close;
> 3, 3, door, 1, 1;

so every one is in a separate line you got to fix your regex by adding the multiline ( /m or (?m) ) modifier so this will match

(?m)(((?:\s*^>\s*[0-9]+\s*,\s*[0-9]+\s*,\s*[a-zA-Z]+\s*(,\s*[a-zA-Z]+\s*)*;$\s*)|(?:\s*^>\s*[0-9]+\s*,\s*[0-9]+\s*,\s*[a-zA-Z]+\s*,\s*[0-9]+\s*(,\s*[\-]?[0-9]+\s*)*;$\s*))+)

hope this solves your issue

于 2013-08-07T03:41:33.633 回答
0

对不起,您的正则表达式太长了,我无法阅读...与其聪明并创建一个小的正则表达式,如果您愿意,您可以为每种格式创建一个不同的格式并将所有这些都包装在括号中把管子放在中间。例如, ((\d+)|([a-zA-Z]+))+

编辑:你似乎正在这样做。为了方便起见,重新启动并首先单独编写每个。或者,您可以提供有关格式的更多详细信息,我们可以为您编写:3

于 2013-08-07T02:44:39.940 回答