Solved

regex

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl 101 11 77
crawling gofundme 4 126
Regex rule to match two different url 5 72
Log File Creation with Header and Footer 17 96
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

809 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