RegEx case

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

I need something like :
if ( !~ "(www.)?")

But ..
it will return false for""       which is ok since i want only and to match.
it will return false for""                 which is ok since i want only and to match
it will return true for""           which is ok since i want only and to match
it will return false for"ANYTHING.foobarcom"           which is NOT OK since i want only and to match
Question by:Stampel
  • 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 ( !~ "(www.)?") ]
so that it will return good values when tested against provided values in my exemple.

You can test with :
perl -e 'if ("" !~ "(www.)?") {print "TRUE\n";} else {print "FALSE\n";}'
perl -e 'if ("" !~ "(www.)?") {print "TRUE\n";} else {print "FALSE\n";}'
perl -e 'if ("" !~ "(www.)?") {print "TRUE\n";} else {print "FALSE\n";}'
perl -e 'if ("" !~ "(www.)?") {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 500 total points
ID: 40392555
perl -e 'if ("" !~ /^(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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

828 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