Home > Enterprise >  Regex for allowing apostrophe and period
Regex for allowing apostrophe and period

Time:12-15

I have below regex which is used for removing punctuations from a string. What I need is to allow only apostrophes and periods in between words such as “Zipf’s”, “e.g”.

[^\w\s]

CodePudding user response:

An idea to use non word boundaries (where no word-character touches specified characters).

\B matches at any position between two word characters as well as at any position between two non-word characters ...

[^\w\s.’']|\B[.’']\B

See this demo at regex101

  • Related