简体   繁体   中英

Need java regex to match substring with multiple whitespace, only one punctuation

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.

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.

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.

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

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