coy8149
coy8149

Reputation: 21

regex - keep / remove same character

I want to remove the apostrophes ' but only those between bracket.

My example is something like this :

'keep apostrophes' ('remove apostrophes')

I proceed like this :

[^a-z()\s]

but then it removes all apostrophes. I don't know a way to combine everything together.

Upvotes: 2

Views: 56

Answers (2)

Eder
Eder

Reputation: 1884

Please try the following pattern:

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

The back-reference, $1, will be stripped out of apostrophes.

Regular expression visualization

Working example @ regex101

Update #1

To accomplish your second request, please use the following pattern:

'([^']+)'\s*(?=(?:\sand|\sor|\)))

Regular expression visualization

Working example @ regex101

Note

  • The usage of positive look-ahead in order to accurately match all the appearances of 'words' inside '()'
  • The usage of regular expressions for this task is overkilling. (What programming language are you using to perform this task?)

Upvotes: 2

vks
vks

Reputation: 67968

'(?=[^(]*\))

You can use lookahead here.See demo.Replace by empty string.

https://regex101.com/r/uE3cC4/28

Upvotes: 1

Related Questions