2

There are such expressions like:

name=="sometext"
value!=4

I would like to split such expressions 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?

scdmb
  • 15,091
  • 21
  • 85
  • 128
  • Are the expressions already a string, or are they like that in the code? And why, for what reason, do you want to split them? – Xeo Oct 08 '11 at 11:49
  • This is a different question but the answer is the same for your case: http://stackoverflow.com/questions/5768948/extract-variables-from-string-math-expression/5769005#5769005 – atoMerz Oct 08 '11 at 11:50
  • @Xeo: They are strings, not code. – scdmb Oct 08 '11 at 11:51

1 Answers1

5

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) «*»
FailedDev
  • 26,680
  • 9
  • 53
  • 73