I have this regex example at this link: http://regexr.com/39rr0
Text input:
1. This camera support Monochrome, Neutral, Standard, Landscape and Portrait!
Expected Output on string replace using this regex:
1 This camera support Monochrome Neutral Standard Landscape and Portrait
I'm trying to remove punctuation which comes after words so I get only the words without it. My regex is (([\\S]+)(,|:|;|\\?|!))
and it matches fine in that regex editor. However when I do string.replace( (([\\S]+)(,|:|;|\\?|!)), "")
or even string.find( (([\\S]+)(,|:|;|\\?|!)) )
it finds nothing.
What's the regex to do this? Is my regex broken or am I using it incorrectly.
You cannot use String.replace
, it only replaces literals.
Try a simpler Pattern
and String.replaceAll
:
System.out.println(
"1. This camera support Monochrome, Neutral, Standard, Landscape and Portrait!"
.replaceAll("\\p{Punct}", "")
);
Output
1 This camera support Monochrome Neutral Standard Landscape and Portrait
Note
If you only need to replace punctuation after a word character (an alphanumeric), you can improve the Pattern
as such:
"(?<=\\w)\\p{Punct}+"
In your case, it will produce the same output.
Use \\\\p{P}
to match all the punctuations.
String str = "1. This camera support Monochrome, Neutral, Standard, Landscape and Portrait!";
System.out.println(str.replaceAll("\\p{P}(?=\\s|$)", ""));
Output:
1 This camera support Monochrome Neutral Standard Landscape and Portrait
Explanation:
\\\\p{P}
Match all the punctuations (?=\\\\s|$)
Only if it's followed by a space or end of the line anchor. Use replaceAll, it works with regexp
System.out.println("This camera support Monochrome, Neutral, Standard, Landscape and Portrait!".replaceAll("([\\S]+)(,|:|;|\\?|!)", "$1"));
Prints :
This camera support Monochrome Neutral Standard Landscape and Portrait
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.