Java正则表达式匹配具有撇号的单词 [英] Java regex match across words having apostrophe

查看:60
本文介绍了Java正则表达式匹配具有撇号的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一句话不,没关系滞后",我试图检测到无滞后"模式.我的正则表达式是 no + (\\s\\w*?\\s?){1,3} + lag.这失败了.但是,如果我的句子是no its all right lag"(注意它的单词没有撇号),则匹配成功.谁能建议我如何忽略窗口中的撇号.我正在使用 java 模式 matcher.

I have a sentence "no it's all right lag" where I am trying to detected a pattern "no lag". My regex is no + (\\s\\w*?\\s?){1,3} + lag. This fails. However if my sentence is "no its all right lag" (note that the word its is not having the apostrophe), then the match succeeds. Can anyone suggest how can i ignore the apostrophe in the window. I am using java pattern matcher.

推荐答案

你可以用 [] 符号创建一个新的字符类,而不是使用 \w 你应该使用 [\w'] 使整个正则表达式no(\s[\w']*?\s?){1,3}lag" 作为 Java 字符串.

You could create a new character class with the [] notation, instead of having \w you should use [\w'] making the entire regex "no(\s[\w']*?\s?){1,3}lag" as a Java String.

同样用于开发正则表达式,我会推荐这个站点:http://www.regexplanet.com/advanced/java/index.html

Also for developing regular expressions I would recommend this site: http://www.regexplanet.com/advanced/java/index.html

这篇关于Java正则表达式匹配具有撇号的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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