拆分表达式,但留下分隔符

Split expressions but leave separators

本文关键字:分隔符 表达式 拆分      更新时间:2023-10-16

有这样的表达式:

name=="sometext"
value!=4

我想用像"=="answers"! "这样的字符串分隔符分割这样的表达式。="并保留这些分隔符,所以结果将是:

name=="sometext"  ->  [name] [==] ["sometext"]
value!=4          ->  [value] [!=] [4]

如何用Boost或其他库完成?

对于boost,我会使用这个简单的正则表达式:

(.*?)(==|!=)(.*)

Edit: 提供表达式为字符串。

编辑2:解释正则表达式

// (.*?)(==|!=)(.*)
// 
// Match the regular expression below and capture its match into backreference number 1 «(.*?)»
//    Match any single character that is not a line break character «.*?»
//       Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
// Match the regular expression below and capture its match into backreference number 2 «(==|!=)»
//    Match either the regular expression below (attempting the next alternative only if this one fails) «==»
//       Match the characters “==” literally «==»
//    Or match regular expression number 2 below (the entire group fails if this one fails to match) «!=»
//       Match the characters “!=” literally «!=»
// Match the regular expression below and capture its match into backreference number 3 «(.*)»
//    Match any single character that is not a line break character «.*»
//       Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»