Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Test condition for pattern match

Posted on 2007-11-21
6
Medium Priority
?
515 Views
Last Modified: 2008-02-07
Please consider following code snippet.  The value of file_path is ..\..\..\src\app\test\filename.c.  What is
[ \  /   \  \ ].  Note:  There is no whitespace inside brackets.  I put whitespace so contents inside parentheses is visible to the experts.  Is the if condition true?
paths_hash is a hash variable.  build_path is M:\view_name\lib\subdir\target\app\test
What does the statement inside if condition do?

if ( $file_path !~ m/[\  /  \  \  ]/ )
{
    $paths_hash{$build_path} = 1;
}
0
Comment
Question by:naseeam
[X]
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
6 Comments
 
LVL 39

Accepted Solution

by:
Adam314 earned 800 total points
ID: 20329352
The if statement is looking to see if $file_path contains either a forward slash "/" or a backslash "\".
So when $file_path = "..\..\..\src\app\test\filename.c", the if condition is true.
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 20329892
Adam, I think you meant the opposite.

The if statement is looking to see if $file_path does NOT contain either a forward slash "/" or a backslash "\".

!~ vs =~ operator
0
 
LVL 1

Author Comment

by:naseeam
ID: 20329918
So forward and backward slash can be in any position.  It doesn't matter that there are two dots before the backward slash.  

Thanks.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 28

Assisted Solution

by:FishMonger
FishMonger earned 640 total points
ID: 20330041
Yes, the forward or backslash could be at any point in the path.  If either are found, then the result of the conditional is false.

Both of these conditionals will have the same results

if ( $file_path !~ m/[\  /  \  \  ]/ )

unless ( $file_path =~ m/[\  /  \  \  ]/ )
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 320 total points
ID: 20330860
The [] is a character class, it mean match ANY of the chars inside it. In this case the chars are \ and / (both escaped) so it'll match either. The != negates this logic so, as stated above, it means don't match.

So the code means...

if no match is found for \ or / in $file_path then $paths_hash{$build_path} equals 1.

More info on character classes: -

http://www.regular-expressions.info/charclass.html

-Rx.

0
 
LVL 17

Assisted Solution

by:mjcoyne
mjcoyne earned 240 total points
ID: 20341696
In summation, $paths_hash{$build_path} is set to equal 1 if the variable $file_path does not contain any forward or backward slashes.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

688 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