So, I was messing around with regex to convert sentences into pig latin, and decided to extend the assignment to allow for punctuation.
I was looking for a regex that allowed for me to replace the punctuation with an empty string and found myString.replaceAll("\\p{P}", "");
and was curious as to what the \p and {P} actually do here. Other similar questions have used "\\p{Z}"
to replace whitespace, which leads me to think the \p is searching for whatever is inside of the brackets. Anyways any clarifications or directions to documentation would be much appreciated.
Asked
Active
Viewed 2,341 times
-2
![](../../users/profiles/8642594.webp)
user8642594
- 48
- 9
-
2The javadoc explains this: https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html – Stephen C May 12 '18 at 01:36
-
1don't know how I missed this. Thank you – user8642594 May 12 '18 at 01:50
1 Answers
1
In PCRE regular expressions
\p{P}
is "Any punctuation character"\p{Z}
is "Any whitespace character"
See the "EXPLANATION" section on the right: https://regex101.com/r/ZFIKpv/1
![](../../users/profiles/9776641.webp)
Shamus
- 66
- 1