Solved

RegEx case

Posted on 2014-10-20
4
228 Views
Last Modified: 2014-10-20
I want to write the most simple perl condition to test URL against, www.foobar.com using !~ operator without using || and multiple tests.

I need something like :
if (req.http.host !~ "(www.)?foobar.com")

But ..
it will return false for req.http.host="www.foobar.com"       which is ok since i want only www.foobar.com and foobar.com to match.
it will return false for req.http.host="foobar.com"                 which is ok since i want only www.foobar.com and foobar.com to match
it will return true for req.http.host="ANYTHING.com"           which is ok since i want only www.foobar.com and foobar.com to match
it will return false for req.http.host="ANYTHING.foobarcom"           which is NOT OK since i want only www.foobar.com and foobar.com to match
0
Comment
Question by:Stampel
  • 2
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 40392371
/^(www\.)?foobar\.com$/
0
 
LVL 7

Author Comment

by:Stampel
ID: 40392531
This is not working.
I want the reg expression corrected in this expression :
 [if (req.http.host !~ "(www.)?foobar.com") ]
so that it will return good values when tested against provided req.http.host values in my exemple.

You can test with :
perl -e 'if ("www.foobar.com" !~ "(www.)?foobar.com") {print "TRUE\n";} else {print "FALSE\n";}'
=> FALSE
perl -e 'if ("foobar.com" !~ "(www.)?foobar.com") {print "TRUE\n";} else {print "FALSE\n";}'
=> FALSE
perl -e 'if ("ANYTHING.com" !~ "(www.)?foobar.com") {print "TRUE\n";} else {print "FALSE\n";}'
=> TRUE
perl -e 'if ("ANYTHING.foobar.com" !~ "(www.)?foobar.com") {print "TRUE\n";} else {print "FALSE\n";}'
=> FALSE THIS ONE IS WRONG, Fix REGEX in the IF to make it returns TRUE
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 40392555
perl -e 'if ("ANYTHING.foobar.com" !~ /^(www\.)?foobar\.com$/ ) {print "TRUE\n";} else {print "FALSE\n";}'
TRUE
0
 
LVL 7

Author Comment

by:Stampel
ID: 40392671
OK that make sense, i guess the problem is in my script because of " "
When used in a script i have to remove beginning and trailing "/" from input_regexp and it finally works as expected.

#!/usr/bin/perl
$input_string = $ARGV[0];
$input_regexp = '^(www\.)?foobar\.com$';
if ( $input_string !~ $input_regexp) {print "FALSE\n";} else {print "TRUE\n"};

I will test in my varnish config to see if it works as expected
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to mount an LVM partition that is part of a disk image. 3 73
AD LDAP LDS 3 66
is my large folder zipped corrupted 4 66
Choosing CentOS 16 81
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question