Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Test condition for pattern match

Posted on 2007-11-21
6
Medium Priority
?
520 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
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
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.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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

972 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