in reply to Re: Regex conditional match if previous match in same expression is true?
in thread Regex conditional match if previous match in same expression is true?
Well, that's certainly an improvement -- I had forgotten about all of the behaviors of the ? modifier. However, I must still be missing something. My code now reads:
use strict; use warnings; for ( 'oh {hello} there', 'oh {hello there', 'oh hello there', 'oh hello} there', ) { print '', ( $_ =~ / (\{)? # optional opening brace hello # .. followed by 'hello' (?(1)\}) # a closing brace iif the open brace was the +re /x ? 'YEP ' : 'NOPE' ), " - $1\n"; }
But my output is:
YEP - {
YEP -
YEP -
YEP -
However, now warnings are thrown for using an uninitialized '$1' in the last three cases, as it should be.
What I'm struggling with is how to say that '{hello}' is OK, and 'hello' is OK, but '{hello' and 'hello}' are NOT OK. Or, put another way, I want to require a closing brace if an opening brace is found; however, if there is no opening brace then there must be no closing brace either.
Thanks for the help, though, it's an improvement.
|
|---|