Pattern for apostrophe inside quotes

后端 未结 2 795
無奈伤痛
無奈伤痛 2021-01-28 06:02

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

2条回答
  •  伪装坚强ぢ
    2021-01-28 06:17

    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

提交回复
热议问题