[英]Regular expression to match this query
I have a sentence like this: 我有这样一个句子:
Well, {hero}Superman X. 123 Sr.{/hero}, the most lovable guy was hated by {lover}Louis{/lover}.
I am using java regular exp. 我正在使用Java Regular Exp。 like this (which is not working: of course): 像这样(不起作用:当然):
Pattern search = Pattern.compile("}.*{\\/")
Actually it provides me this output: 实际上,它为我提供了以下输出:
}Superman X. 123 Sr.{/hero}, the most lovable guy was hated by {lover}Louis{/
When actually I want: "Superman X. 123 Sr." 我实际上何时想要:“超人X.123 Sr.” and then "Louis". 然后是“路易” How can this be achieved apart from running a while loop and increment the index? 除了运行while循环并增加索引外,如何实现? I can try that ..but was trying to know if there is an easier way that I am missing. 我可以尝试..但是试图知道是否有更简单的方法被我遗漏。
There may be a better regex, but this (\\{\\w+\\})([\\w\\.\\s]+)(\\{/\\w+\\})
does your work: 可能会有更好的正则表达式,但是这个(\\{\\w+\\})([\\w\\.\\s]+)(\\{/\\w+\\})
完成您的工作:
String test = "Well, {hero}Superman X. 123 Sr.{/hero}, the most lovable guy"+
" was hated by {lover}Louis{/lover}.";
Pattern p = Pattern.compile("(\\{\\w+\\})([\\w\\.\\s]+)(\\{/\\w+\\})");
Matcher m = p.matcher(test);
while(m.find()){
System.out.println(m.group(2));
}
That is because quantifiers are greedy by default. 那是因为默认情况下量词是贪婪的。 You want a lazy quantifier, so try .*?
您想要一个懒惰的量词,所以尝试.*?
instead of just .*
. 而不是.*
。
Also, you might want to capture the tag itself: 另外,您可能希望捕获标签本身:
Pattern.compile("\\{([^}]+)\\}(.*?)\\{/\1\\}");
Note that I'm not 100% certain of the current syntax for a backreference in Java regexes, but that should work. 请注意,我对Java正则表达式中的反向引用不是100%肯定当前语法,但是应该可以。 You should end up with the tag name in the first captured subpattern ( hero
or lover
in this case), and the name itself in the second subpattern. 您应该在第一个捕获的子模式(在这种情况下为hero
或lover
中以标签名称结尾,在第二个子模式中以标签名称结尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.