G
Grost
Hi all,
I'm writing an application to perform some HTML text manipulation from
templates and I have a regex formulation problem. For example, in the
template I have a line:
<tr><td class="caption"><!--@caption--><!--<br />(@caption)--></td></tr>
where the parts I want to replace are HTML comments: <!-- ??? -->
There are two styles of comment I want to search/replace:
1) <!--@caption-->
2) <!--XXX(@caption)YYY-->, where XXX and YYY can represent other HTML
Case 1 is easy, and I just use: <!--\s*?@caption\s*?-->
Case 2 is the problem. I trying to use this for conditional insertion
of additional HTML, depending on whether @caption exists in the
application. If I have a value for @caption, then the following is
produced from the above example:
<tr><td class="caption">foo<br />foo</td></tr>
This seems easy enough in principle, but every regex pattern I've tried
unsuprisingly matches the <!-- from the first comment. My initial try
which of course failed was: <!--(.*?)\(@caption\)(.*?)-->
What I need is a way of saying:
Match "(@caption)" within an HTML comment, and capture the text on
either side of tag and within the comment, but make sure there are no
other comment-like tags within that text. I'm guessing I need something
along the lines of the lookaround operators, but I have little
experience with them. Any help anyone...?
(For clarity I removed the extra escaping required for Java inline strings.)
Stan
I'm writing an application to perform some HTML text manipulation from
templates and I have a regex formulation problem. For example, in the
template I have a line:
<tr><td class="caption"><!--@caption--><!--<br />(@caption)--></td></tr>
where the parts I want to replace are HTML comments: <!-- ??? -->
There are two styles of comment I want to search/replace:
1) <!--@caption-->
2) <!--XXX(@caption)YYY-->, where XXX and YYY can represent other HTML
Case 1 is easy, and I just use: <!--\s*?@caption\s*?-->
Case 2 is the problem. I trying to use this for conditional insertion
of additional HTML, depending on whether @caption exists in the
application. If I have a value for @caption, then the following is
produced from the above example:
<tr><td class="caption">foo<br />foo</td></tr>
This seems easy enough in principle, but every regex pattern I've tried
unsuprisingly matches the <!-- from the first comment. My initial try
which of course failed was: <!--(.*?)\(@caption\)(.*?)-->
What I need is a way of saying:
Match "(@caption)" within an HTML comment, and capture the text on
either side of tag and within the comment, but make sure there are no
other comment-like tags within that text. I'm guessing I need something
along the lines of the lookaround operators, but I have little
experience with them. Any help anyone...?
(For clarity I removed the extra escaping required for Java inline strings.)
Stan