Solved

regex

Posted on 1998-10-03
3
163 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
  • 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
execute pl/sql block from perl program 3 111
perl to mysql 5 128
Perl Regular expression 9 190
File Find regex problem 4 58
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…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now