Reputation: 6554
I have this regex code:
<!--\s?BEGIN (\w+) \s?-->(.*?)<!--\s?END \w+\s?-->
Which will match
<!-- BEGIN some_functional_name -->
//some stuff here later
<!-- BEGIN some_functional_name -->
What I need is to watch out for nested comments and if the names are the same. For example
<!-- BEGIN some_functional_name -->
//some stuff here later
<!-- BEGIN another_functional_name -->
//it'll match up to the below end another_functional_name
<!-- END another_functional_name -->
<!-- END some_functional_name -->
As you can see it's nested but the regex will capture the second to last END
comment which the BEGIN
and END
do not match. Is there a method to get inline regex capture groups?
Example:
<!--\s?BEGIN (\w+) \s?-->(.*?)<!--\s?END $1\s?-->
Though I'm also clueless on how to handle nesting? Should I use a positive lookaround or what?
Upvotes: 1
Views: 61
Reputation: 626845
You should use a backreference, e.g. '\1'.
<!--\s?BEGIN (\w+) \s?-->(.*?)<!--\s?END \1\s?-->
Here is an example.
Upvotes: 1