Regex how to match all punctuations but exclude some conditions

后端 未结 2 1048
逝去的感伤
逝去的感伤 2021-01-14 00:13

I can use \\p{Punct} to match all punctuations(including underscore).

And I wanted to exclude all apostrophes strictly inside a word.

2条回答
  •  半阙折子戏
    2021-01-14 00:47

    You can combine three conditions here.

    1. Match all punctuation except apostrophe ' using [\p{Punct}&&[^']]

    2. Match all apostrophe not followed by a letter.

    3. Match all apostrophe not preceded by a letter.

    Regex: [\p{Punct}&&[^']]|(?

    Explanation:

    • [\\p{Punct}&&[^']] excludes apostrophe from punctuation class.

    • (? matches apostrophe not preceded by a letter.

    • '(?![a-zA-Z]) matches the apostrophe not followed by a letter.

    RegexPlanet Fiddle

提交回复
热议问题