Разделить выражения, но оставить разделители - PullRequest
2 голосов
/ 08 октября 2011

Существуют такие выражения, как:

name=="sometext"
value!=4

Я хотел бы разделить такие выражения с помощью разделителей строк, таких как "==" и "! =", И сохранить эти разделители, поэтому результатом будет:

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

Как это можно сделать с помощью Boost или какой-либо другой библиотеки?

1 Ответ

5 голосов
/ 08 октября 2011

С бустом я бы использовал это простое регулярное выражение:

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

Редактировать: при условии , что выражение является строкой.

Редактировать 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) «*»
...