0

I am looking for a pattern that can find apostrophes that are inside single quotes. For example the text

Foo 'can't' bar 'don't'

I want to find and replace the apostrophe in can't and don't, but I don't want to find the single quotes

I have tried something like

(.*)'(.*)'(.*)'

and apply the replace on the second matching group. But for text that has 2 words with apostrophes this pattern won't work.

Edit: to clarify the text could have single quotes with no apostrophes inside them, which should be preserved as is. For example

'foo' 'can't' bar 'don't'

I am still looking for only apostrophes, so the single quotes around foo should not match

JCWong
  • 1,163
  • 1
  • 10
  • 29

2 Answers2

2

I believe you need to require "word" characters to appear before and after a ' symbol, and it can be done with a word boundary:

\b'\b

See the regex demo

To only match the quote inside letters use

(?<=\p{L})'(?=\p{L})
(?<=[[:alpha:]])'(?=[[:alpha:]])
(?U)(?<=\p{Alpha})'(?=\p{Alpha})  # Java, double the backslashes in the string literal

Or ASCII only

(?<=[a-zA-Z])'(?=[a-zA-Z])
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
1

You can use the following regular expression:

'[^']+'\s|'[^']+(')[^' ]+'

it will return 3 matches, and if capture group 1 participated in the word, it will be the apostrophe in the word:

  • 'foo'
  • 'can't'
  • 'don't'

demo

How it works:

  • '[^']+'\s
    • ' match an apostrophe
    • [^']+ followed by at least one character that isn't an apostrophe
    • ' followed by an apostrophe
    • \s followed by a space
  • | or
  • '[^']+(')[^' ]+'
    • ' match an apostrophe
    • [^']+ followed by at least one character that isn't an apostrophe
    • (') followed by an apostrophe, and capture it in capture group 1
    • [^' ]+ followed by at least one character that is not an apostrophe or a space
    • ' followed by an apostrophe
Keith Hall
  • 15,362
  • 3
  • 53
  • 71