michael
michael

Reputation: 105

RegEx: Ignoring only characters that meet both lookahead and lookbehind

I'm trying to parse a file in Ruby to make it valid JSON. It uses single quotes, so I need to replace those.

But it also uses single quotes as possessives in words. I'm trying to use regex to get only single quotes that don't have a letter before AND a letter after, but what I have so far is ignoring anything with a letter before OR a letter after.

RegEx (?<![a-zA-Z])'(?![a-zA-Z])

Test String 'id': '4da7680f-3449-303f-8e79-d2abe665cbe9', 'phone_numbers': ['4108286112']}]}, {'email': None, 'gender': '', 'first_name': None, 'id': 1659439495, 'last_name': None, 'middle_name': None, 'network_ids': [100494], 'organization_name': "CHARLOTTE'S HAIR LINE INC.", 'phone': '4109334800'

Link: https://rubular.com/r/MDthi9TWEvLOip

Upvotes: 2

Views: 39

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626758

You can use the following pattern:

'(?!(?<=[a-zA-Z]')[a-zA-Z])

See the Rubular demo

Details

  • ' - a single quotation mark
  • (?!(?<=[a-zA-Z]')[a-zA-Z]) - a negative lookahead that fails the match if, immediately to the right of the current location (after '), there is
    • (?<=[a-zA-Z]') - (a positive lookbehind matching) a location that is immediately preceded with an ASCII letter and '
    • [a-zA-Z] - an ASCII letter.

Upvotes: 2

Related Questions