Java正则表达式第一个匹配的字符
String text = "ref=\"ar\" alias=\"as sa\" value=\"asa\"";
实际上我想获取 ref 和 alias 的双引号之间的所有数据的值。也框架了正则表达式。但我面临的问题是对于别名来说,它不匹配第一个双引号,而是匹配最后一个双引号。我只想要第一个双引号内的数据
String patternstr="(alias=\".*\")|(ref=\"[[\\w]]*\")";
String patternstr2Level="\".*\"";
在第一个匹配中将获取两个参数,在第二个匹配中将获取引号中的数据
当前结果:
“ar”
“as sa”value =“asa”
所需结果:
“ar”
“作为萨”
String text = "ref=\"ar\" alias=\"as sa\" value=\"asa\"";
Actually i want get the value of all the data between the double quotes of ref and alias . Have framed the regular expression too. But the prob i am facing is for alias it is not matching the first double quotes but the last one. I want data only upto the first double quotes
String patternstr="(alias=\".*\")|(ref=\"[[\\w]]*\")";
String patternstr2Level="\".*\"";
In first matching the two parameter will be acquired and in the second matching data in quotes will be acquired
Current Result:
"ar"
"as sa" value="asa"
Required Result:
"ar"
"as sa"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你只需要让你的比赛变得懒一点。我相信这
应该能解决问题。通过使用
.*?
而不仅仅是.*
,匹配的该部分会变得惰性。换句话说,它将尝试匹配找到的第一个双引号,而不是匹配尽可能多的内容,直到到达最后一个双引号。我用 Python 测试了它,效果很好。You just need to make your match a little bit lazier. I believe that
should do the trick. By using
.*?
instead of just.*
, that part of the match becomes lazy. In other words, it will try to match the first double quote that it finds rather than matching as much stuff as possible until it gets to the last double quotes. I tested it in Python and it worked great.尝试
String patternstr="(alias=\"[\\w\\s]*\")|(ref=\"[[\\w]]*\")";
Try
String patternstr="(alias=\"[\\w\\s]*\")|(ref=\"[[\\w]]*\")";
也匹配最后一个,但使用 ?> 修改要从结果中排除的组修饰符。
有关更多信息,请参阅:
http://www.regular-expressions.info/atomic.html
match the last one as well but modify the group to be excluded from the result with the ?> modifier.
See this for more info:
http://www.regular-expressions.info/atomic.html