正则表达式通过布尔运算符拆分布尔字符串
Regex split boolean string by boolean operators
是否有正则表达式通过布尔运算符拆分布尔字符串但不在引号内
示例: boolean_string = "'larsen and turbo' and fullsta'ck or \"ruby\" and \"ruby or rails\""
预期输出: ['larsen and turbo', and, fullsta'ck, or, ruby, and, ruby or rails]
目前正在尝试使用以下代码片段,但无法在引号内转义运算符。
boolean_string.split(/\b(and|or)\b/)
如何在引号内转义 and
和 or's
。`
这可能对您有帮助:
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"]
是否有正则表达式通过布尔运算符拆分布尔字符串但不在引号内
示例: boolean_string = "'larsen and turbo' and fullsta'ck or \"ruby\" and \"ruby or rails\""
预期输出: ['larsen and turbo', and, fullsta'ck, or, ruby, and, ruby or rails]
目前正在尝试使用以下代码片段,但无法在引号内转义运算符。
boolean_string.split(/\b(and|or)\b/)
如何在引号内转义 and
和 or's
。`
这可能对您有帮助:
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"]