Bug 53387

Summary: SSI: Allow to use $1 to get result of regular expression matching
Product: Tomcat 7 Reporter: Konstantin Kolinko <knst.kolinko>
Component: CatalinaAssignee: Tomcat Developers Mailing List <dev>
Status: RESOLVED FIXED    
Severity: enhancement CC: david.reitter
Priority: P2    
Version: 7.0.27   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   

Description Konstantin Kolinko 2012-06-08 12:14:41 UTC
This was originally reported in bug 48956, but it is a separate issue.

In Apache HTTPD there is such example in documentation of mod_include: [1]

[[[
<!--#if expr="$QUERY_STRING = /^sid=([a-zA-Z0-9]+)/" -->
<!--#set var="session" value="$1" -->
<!--#endif -->
]]]

[1] http://httpd.apache.org/docs/current/mod/mod_include.html

Here $1 references the value of group #1 as caught by regular expression.

Example from comment 3 in bug 48956:
[[[
<!--#set var="aux" value="aa12" -->
<!--#if expr="$aux=/^aa([a-zA-Z0-9\-_]*)/" -->
        <!--#set var="aux2" value="$1" -->
<!--#endif -->

Resulting value:<!--#echo var="aux2" -->
]]]

In Tomcat support for regular expression in SSI was added in r1136231 and r1136399 and is implemented in  ExpressionParseTree$CompareNode#compareBranches()

Maybe the last used Matcher can be stored in some context, so that it can be later used to resolve $1 etc. references. I do not know whether $1 is limited to that #if, or it is available everywhere later until it is overwritten by the next regexp-matching operation. I do not know how HTTPD behaves here. I think the latter would be easier to implement.
Comment 1 Mark Thomas 2013-07-01 20:08:30 UTC
*** Bug 55176 has been marked as a duplicate of this bug. ***
Comment 2 Mark Thomas 2018-06-26 16:52:48 UTC
I've tested httpd and the behaviour is as follows:

Before any matches are attempted, $1 and friends are set to the empty string
After a successful match, $1 and friends remember the contents of the group until the next match is attempted (not is successful)
After an unsuccessful match, $1 and friends are set to the empty string

At first glance, this looks fairly easy to implement.
Comment 3 Mark Thomas 2018-06-27 10:16:47 UTC
Implemented in 9.0.x for 9.0.11 onwards.