简体   繁体   中英

Pattern matching in Java with punctuations

I want to provide support to match both punctuations and regex characters in Java. The support for punctuations is working fine, but it is failing for even simple regexes.

Here's my pattern:

(\s\p{Punct}|^\p{Punct}|\s|^)ros?(\s|\p{Punct}|$)

And content I'm trying to match: rose

find() method is returning false for this pattern. I initially thought that root cause was the {Punct} part of the pattern so I tried changing my regex to (\\s\\p{Punct}|^\\p{Punct}|\\s|^)ro?e(\\s|\\p{Punct}|$) in my unit test, but even that is failing

Your pattern does not match because the 'e' in "rose" is not a punctuation or whitespace character.

Besides the ros? in your pattern makes only the s only optional.

Note: The ? and * in Unix shell is not a "Linux regex", it is a "shell globbing" wildcard. Regular expressions in Linux (grep, sed, awk) all use . for a single replace char.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM