拆分表达式但保留分隔符

时间:2011-10-08 11:45:45

标签: c++ split

有这样的表达方式:

name=="sometext"
value!=4

我想用“==”和“!=”之类的字符串分隔符拆分这些表达式并保留这些分隔符,结果将是:

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

如何使用Boost或其他库来完成?

1 个答案:

答案 0 :(得分:5)

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

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

编辑:提供表达式为字符串。

编辑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) «*»