2011-10-08 164 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) «*»