Solved

regex

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl count the hash for print 4 161
perl split by | 2 90
problem with using the glob function 1 63
To run CGI/perl on tomcat and connect to oracle database. 5 118
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…
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…
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

911 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

15 Experts available now in Live!

Get 1:1 Help Now