简体   繁体   中英

Regex (en-US) to find all punctuation EXCEPT if part of word

I've found the regex to locate all hyphenated and "apostrophied" words:

(?=\S*['-])([a-zA-Z'-]+)

Examples: l'Equipe and action-oriented

I'm not sure how to utilize this information to get what I want after a bit of searching...

Try this regex

(?<=\b)[,.'-:"]+(?=\s|$)|"

Demo: https://regex101.com/r/mDhl17/1/

Here are some details

  • This regex will start with Positive Lookbehind with a word boundary token in order to exclude any punctuation that has letters before it.
  • We add the targeted punctuation mark afterward [,.'-:"]+ . You can add to the list whatever you prefer to catch.
  • We expect the punctuation marks to be followed by either a space or to be at the end of the sentence (?=\\s|$) .
  • An exception to this, is the quotation marks " " , we want to catch them whenever they are there, so I added the OR | condition at the end.

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