0

Is there a regex to split boolean string by boolean operator but not within quotes

example: boolean_string = "'larsen and turbo' and fullsta'ck or \"ruby\" and \"ruby or rails\""

expected output: ['larsen and turbo', and, fullsta'ck, or, ruby, and, ruby or rails]

Currently trying with below code snippet but unable to escape operators within quotes. boolean_string.split(/\b(and|or)\b/)

How do I escape and and or's within quotes.`

1 Answer 1

2

This may helps you:

boolean_string = "'larsen and turbo' and fullsta'ck or \"ruby\" and \"ruby or rails\""
reg = %r{
  (?:                # outer-group to use "|" for multiple matches
    ['"]([^'"]+)['"] # words inside quotes
    |([\w']+)        # other words
  )
}xm

boolean_string.scan(reg).flat_map(&:compact) # => ["larsen and turbo", "and", "fullsta'ck", "or", "ruby", "and", "ruby or rails"]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.