tags:

views:

135

answers:

3

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.

+7  A: 

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.

Mark Byers
A: 

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.

The first `+` there refers to something else preceding this part of the regex, and should not be included as part of this answer. The `(\s*)+` is redundant - you should use `\s*`. And the `?` means that the punctuation is optional.
Mark Byers
Looking at your answer more carefully, I think you are misunderstanding what `+` does. `a+b` does not mean 'a followed by b`. It means 'one or more a followed by exactly one b'.
Mark Byers
A: 

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...