Java regex match across words having apostrophe

痞子三分冷 提交于 2021-02-17 07:03:26

问题


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.


回答1:


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.

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




回答2:


I think just no(.*)lag pattern will work. If I understood right.



来源:https://stackoverflow.com/questions/35426560/java-regex-match-across-words-having-apostrophe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!