开发者

Split expressions but leave separators

开发者 https://www.devze.com 2023-04-11 10:08 出处:网络
There are such expressions like: name==\"sometext\" value!=4 I would like to split such exp开发者_如何学Goressions with string separators like \"==\" and \"!=\" and keep these separators, so result

There are such expressions like:

name=="sometext"
value!=4

I would like to split such exp开发者_如何学Goressions with string separators like "==" and "!=" and keep these separators, so result would be:

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

How can it be done with Boost or some other library?


With boost I would use this simple regex :

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

Edit : provided that the expression is a string.

Edit 2 : explanation of the regex

// (.*?)(==|!=)(.*)
// 
// 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) «*»
0

精彩评论

暂无评论...
验证码 换一张
取 消