[英]Using backreference to refer to a pattern rather than actual match
I am trying to write a regex which would match a (not necessarily repeating) sequence of text blocks, eg: 我正在尝试编写一个匹配(不一定重复)文本块序列的正则表达式,例如:
foo,bar,foo,bar
My initial thought was to use backreferences, something like 我最初的想法是使用反向引用,例如
(foo|bar)(,\\1)*
But it turns out that this regex only matches foo,foo
or bar,bar
but not foo,bar
or bar,foo
(and so on). 但事实证明,这个正则表达式只匹配foo,foo
或bar,bar
但不匹配foo,foo
foo,bar
或bar,foo
(依此类推)。
Is there any other way to refer to a part of a pattern? 有没有其他方法可以引用模式的一部分?
In the real world, foo
and bar
are 50+ character long regexes and I simply want to avoid copy pasting them to define a sequence. 在现实世界中, foo
和bar
是50多个字符长的正则表达式,我只是想避免复制粘贴它们来定义序列。
With a decent regex flavor you could use (foo|bar)(?:,(?-1))*
or the like. 有了正常的正则表达式,你可以使用(foo|bar)(?:,(?-1))*
等。 But Java does not support subpattern calls. 但Java不支持子模式调用。
So you end up having a choice of doing String replace/format like in ajx's answer, or you could condition the comma if you know when it should be present and when not. 因此,您最终可以选择在ajx的答案中执行字符串替换/格式化,或者如果您知道它何时应该存在且何时不存在,则可以调整逗号。 For example: 例如:
(?:(?:foo|bar)(?:,(?!$|\s)|))+
Perhaps you could build your regex bit by bit in Java, as in: 也许你可以在Java中逐位构建你的正则表达式,如:
String subRegex = "foo|bar";
String fullRegex = String.format("(%1$s)(,(%1$s))*", subRegex);
The second line could be factored out into a function. 第二行可以分解为函数。 The function would take a subexpression and return a full regex that would match a comma-separated list of subexpressions. 该函数将采用子表达式并返回与逗号分隔的子表达式列表匹配的完整正则表达式。
The point of the back reference is to match the actual text that matches, not the pattern, so I'm not sure you could use that. 后引用的要点是匹配匹配的实际文本,而不是模式,因此我不确定您是否可以使用它。
Can you use quantifiers like: 你可以使用如下的量词:
String s= "foo,bar,foo,bar";
String externalPattern = "(foo|bar)"; // comes from somewhere else
Pattern p = Pattern.compile(externalPattern+","+externalPattern+"*");
Matcher m = p.matcher(s);
boolean b = m.find();
which would match 2 or more instances of foo or bar (followed by commas) 这将匹配2个或更多的foo或bar实例(后跟逗号)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.