mardi 27 septembre 2016

C++11 Regex IfThenElse - Single, closed brackets matched OR no brackets matched

How can I define a c++11/ECMAScript compatible regex statement that matches strings either:

  1. Containing a single, closed, pair of round brackets containing an alphanumeric string of length greater than 0 - for example the regex statement "(\w+)", which correctly matches "(abc_123)" and ignores the incorrect "(abc_123", "abc_123)" and "abc_123". However, the above expression does not ignore input strings containing multiple balanced/unbalanced bracketing - I would like to exclude "((abc_123)", "(abc_123))", and "((abc_123))" from my matched results.

  2. Or a single, alphanumeric word, without any unbalanced brackets - for example something like the regex statement "\w+" correctly matches "abc_123", but unfortunately incorrectly matches with "(abc_123", "abc_123)", "((abc_123)", "(abc_123))", and "((abc_123))"...

For clarity, the required matchings for each the test cases above are:

  • "abc_123" = Match,
  • "(abc_123)" = Match,
  • "(abc_123" = Not matched,
  • "abc_123)" = Not matched,
  • "((abc_123)" = Not matched,
  • "(abc_123))" = Not matched,
  • "((abc_123))" = Not matched.

I've been playing around with implementing the IfThenElse format suggested by http://ift.tt/1dLSWdE, but haven't gotten very far... Is there some way to limit the number of occurrences of a particular group [e.g. "((){0,1}" matches zero or one left hand round bracket], AND pass the number of repetitions of a previous group to a later group [say "num\1" equals the number of times the "(" bracket appears in "((){0,1}", then I could pass this to the corresponding closing bracket group, "()){num\1}" say...]

Aucun commentaire:

Enregistrer un commentaire