2011-10-08 86 views
2

有这样的表达,如:“!=”拆分表达式,但离开分离

name=="sometext" 
value!=4 

我想这样的表达式与字符串分隔符,如“==”和分裂,并保持这些分隔,因此结果将是:

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

怎么可以用Boost或其他库?

+0

的解释是表达已经是一个字符串,或者是他们喜欢的代码?为什么,你为什么要分裂他们呢? – Xeo

+0

这是一个不同的问题,但答案是相同的你的情况:http://stackoverflow.com/questions/5768948/extract-variables-from-string-math-expression/5769005#5769005 – atoMerz

+0

@Xeo:他们是字符串,而不是代码。 – scdmb

回答

5

有了提升,我会用这个简单的正则表达式:

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

编辑:提供的表达是一个字符串。

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