Reputation: 1612
I want to make sure that the substring I am matching only has one possible piece of punctuation and as much whitespace as necessary. This is inside of a much longer REGEX, currently what there is is the following:
[\p{P},\s]
but that will match all punctuation and whitespace, so that it accepts:
the string before,,,, ,,,. ....the string after when what I want it to match is any amount of whitespace in between the string before and the string after, with only one item of punctuation allowed- note that the punctuation can come at the beginning of the string, at the end, or with as much whitespace before or after.
Upvotes: 4
Views: 5749
Reputation: 1612
Yeah you're right it was redundant
it should be
\s*(\p{P})?\s
basically the same as what you put, but has to match 'one possible piece of punctuation' not one required piece of punctuation. The plus signs were put in to indicate that it was part of a longer regex...
Upvotes: 0
Reputation: 838696
what I want it to match is any amount of whitespace in between the string before and the string after, with only one item of punctuation allowed
Try this:
\s*\p{P}\s*
Explanation:
\s* Match any amount of whitespace \p{P} Match a single punctuation character \s* Match any amount of whitespace
Note that in Java string literals the backslashes need escaping.
Upvotes: 9
Reputation: 1612
oops, I think I found it myself - at any rate it seems to work with various combinations of whitespace and punctuation:
+(\s*)+(\p{P})?+(\s)+
with the parts before and after the plus signs being the rest of the string being matched.
Upvotes: 0