Solved

special RE variables

Posted on 2004-08-03
9
189 Views
Last Modified: 2010-03-05
I need to perform the following operation on regular expressions:
I have a string, say, bFFacdABCf, that matches a pattern, say, ab!acd!fac , where ! is a sequence of any letters. I want to be able for each match to create the following pair:
! - FF
! - ABC ...etc

Is there a special variable that can be used as a result of matching? If not, what's the way to do this?
thnx
0
Comment
Question by:ole111
  • 5
  • 3
9 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 11712815
@matches = "abFFacdABCfac" =~ /ab(.*)acd(.*)fac/;
print map"! - $_\n",@matches;
0
 

Author Comment

by:ole111
ID: 11714449
Apparently, I did not express myself properly.
The problem is that I don't know in advance how the pattern will look, i.e. it might be ab!acd!, or abac!d, or a!!cd etc...
Given that there is a match with a string, I still need to be able to create the pairs...
0
 
LVL 20

Expert Comment

by:jmcg
ID: 11715230
Ozo's approach works if you _know_ the pattern, right? So you just need to modify the input pattern, with its ! notation, and change that to be a Perl regular expression that will work like Ozo's.

$pattern = "ab!acd!fac";
$string = "abFFacdABCfac";
$regex_pattern = $pattern =~ s/!/(.*)/g;
@matches = $string =~ /$regex_pattern/;
print "! - $_\n" foreach @matches;

==============

But the string you gave is only a partial match for the pattern. Is that intentional? It makes the problem MUCH more difficult to solve in general. For an example, see the discussion in

http:Q_21065267.html

0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:ole111
ID: 11716877
When I try the line
@matches = $string =~ /$regex_pattern/;
what happens is that the whole string is matched (correctly). print "$_" returns 1.
Is this what should be expected?
What I want is to be able to get out of this match all the cases when ! (which in the regex_pattern would stand for something like \w*) is substituted for whatever it has to be. (Taken the above example, there are 2 such cases: 1. ! is substituted with FF, 2. ! is substituted with ABC). The essential thing for me is to be able to extract all such cases that happen during the match.

0
 
LVL 20

Accepted Solution

by:
jmcg earned 200 total points
ID: 11717410
Forgot some parens:

$pattern = "ab!acd!fac";
$string = "abFFacdABCfac";
($regex_pattern = $pattern) =~ s/!/(.*)/g;
@matches = $string =~ /$regex_pattern/;

print "regex pattern: ", $regex_pattern, "\n";

print "! - $_\n" foreach @matches;


When I run that, I got the results:

regex pattern: ab(.*)acd(.*)fac
! - FF
! - ABC

======

That looks to be what you wanted except for the possible problem of wanting partial matches.
0
 

Author Comment

by:ole111
ID: 11717681
Yeap, thnx.
Can I have one more small Q (for the same price :)
If I want to split /usr/bin/ I cannot write
split (/// "/usr/bin/");
what is the correct way?
0
 
LVL 20

Expert Comment

by:jmcg
ID: 11717913
There's two approaches.

  split /\//, "/usr/bin/";

uses backslash inside the regex to make the following slash a literal rather than allowing it to be interpreted as a closing delimiter.

  split m{/}, "/usr/bin/";

is just one example of the other approach. Most regular expression grammars allow you to use alternate delimiters. This was even true of the UNIX 'ed' editor 30-some years ago. I tend to use curly braces except in situations where I'd have to put a backslash in front of a curly brace in the regex. For { }, [ ], ( ), and < >, the delimiters work as matched pairs. For other characters, you just use the same character to open and close.

   split m#/#, "/usr/bin/";
   split m!/!, "/usr/bin/";
   split m</>, "/usr/bin/";

By the way, if you split that particlar string with m{/}, you'll get one empty element at the front, then "usr", and finally "bin". There's no empty element at the end, since Perl lists are trimmed at the last non-empty element in this and a number of other contexts.
0
 

Author Comment

by:ole111
ID: 11718020
that's ok, i found it myself. thnx
0
 

Author Comment

by:ole111
ID: 11718030
oh , i did not realize there was a new message. thanks for the answer.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
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…

815 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

11 Experts available now in Live!

Get 1:1 Help Now