简体   繁体   English

正则表达式将 boolean 字符串拆分为 boolean 运算符

[英]Regex split boolean string by boolean operators

Is there a regex to split boolean string by boolean operator but not within quotes是否有正则表达式将 boolean 字符串拆分为 boolean 运算符但不在引号内

example: boolean_string = "'larsen and turbo' and fullsta'ck or \"ruby\" and \"ruby or rails\""示例: 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]预期 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.`我如何在引号内转义andor's 。`

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"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM