logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Apache View previous topic :: View next topic
Reply to topic   Topic: Using regex results, if backreference exists include FOO
Author
csdude55



Joined: 22 Jan 2023
Posts: 23
Location: USA, Wilkesboro

PostPosted: Tue 21 Feb '23 23:56    Post subject: Using regex results, if backreference exists include FOO Reply with quote

In plain English, I'm trying to say "if 'string' starts with 'abc', set a variable of '<b>abc</b>remainder'. If it doesn't, set a variable of the entire value".

Surrounding "abc" with <b></b> is where I get hung up. The only way I can figure to do this in Apache is with two SetEnvIfExpr:

Code:
# not tested, please forgive any typos
SetEnvIfExpr "env('string') =~ /(^abc)(.+)/" var=<b>$1</b>$2

SetEnvIfExpr "-z env('var') && env('string') =~ /(.+)/" var=$1


In Perl I would probably do something like:

Code:
$string =~ s/^(abc)(.+)/foo($1)/ei;

sub foo {
  return ($_[0] eq 'abc') ?
    '<b>abc</b>' :
    $_[0];
}


Is the best method in Apache to use two SetEnvIfExpr like that?
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7371
Location: Germany, Next to Hamburg

PostPosted: Wed 22 Feb '23 10:55    Post subject: Reply with quote

If the URL query starts with abc, set your variable to abc

Code:

<LocationMatch ^/i.php>
    <If "%{QUERY_STRING} =~ /^abc/">
        SetEnv var "abc"
    </If>
</LocationMatch>
Back to top
covener



Joined: 23 Nov 2008
Posts: 59

PostPosted: Tue 28 Feb '23 4:42    Post subject: Re: Using regex results, if backreference exists include FOO Reply with quote

you can use negative lookahead to simplify (in some sense)


Code:
SetEnvIf string (^abc)(.+) var=<b>$1</b>$2
SetEnvIf string ^(?!abc)(.+) var=$1
# testing only
SetEnvIfExpr "%{QUERY_STRING} =~ /(.*)/" string=$1
Header always set string %{string}e env=string
Header always set var %{var}e env=var

Code:

$ wget -S http://localhost/  http://localhost/?xxabc http://localhost/?abcdef 2>&1 |egrep "(var|string)"
  string:
  string: xxabc
  var: xxabc
  string: abcdef
  var: <b>abc</b>def
Back to top


Reply to topic   Topic: Using regex results, if backreference exists include FOO View previous topic :: View next topic
Post new topic   Forum Index -> Apache