Solved

regex

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
syslog unix file 20 76
Convert grep lines to perl 6 59
Perl modules on linux ec2 3 114
How to get all the API from website? 11 106
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…
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…

830 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