Solved

regex

Posted on 1998-10-03
3
171 Views
Last Modified: 2010-03-05
The below two snippets of code should do the SAME thing, however the first works and the later one doesn't. What is wrong with my regex in the second?

--- This WORKS: ----------------------------
 if (($a =~ /\//) && ($b =~ /\//)) {
   ($tempA,$junk) = split(/\//,$a);
   ($tempB,$junk) = split(/\//,$b);
   print "($tempA : $tempB)\n";
 }
--------------------------------------------

---  This DOESN'T: -------------------------
 if (($a =~ /(.*)\//) && ($b =~ /(.*)\//)) {
   print "($1 : $2)\n";
 }
--------------------------------------------
0
Comment
Question by:rw263
[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
  • 2
3 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1205128
Nothing sets $2, and the $1 from ($a =~ /(.*)\//) is overwritten by ($b =~ /(.*)\//))
You might try something like
print "($tempA : $tempB)\n" if (($tempA)=($a =~ /(.*)\//)) && (($tempB)=($b =~ /(.*)\//));
0
 

Accepted Solution

by:
ItamarC earned 10 total points
ID: 1205129
The comment from 'ozo' was right. If you want to use $1 and $2, you need to have all this in one matching expression.
The code below will do what you need. I used "x_x" to separate $a and $b. You can change it to what you like.

if ( "${a}x_x$b" =~ /(.*)\/.*x_x(.*)\// ) {
  print "($1 : $2)\n";
}

0
 
LVL 84

Expert Comment

by:ozo
ID: 1205130

0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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

691 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