RegEx case

Posted on 2014-10-20
Medium Priority
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
Question by:Stampel
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 84

Expert Comment

ID: 40392371

Author Comment

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";}'
perl -e 'if ("foobar.com" !~ "(www.)?foobar.com") {print "TRUE\n";} else {print "FALSE\n";}'
perl -e 'if ("ANYTHING.com" !~ "(www.)?foobar.com") {print "TRUE\n";} else {print "FALSE\n";}'
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
LVL 84

Accepted Solution

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

Author Comment

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.

$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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.
Suggested Courses
Course of the Month12 days, 15 hours left to enroll

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