Solved

special RE variables

Posted on 2004-08-03
9
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

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
perl script to search for files containing text 3 91
Union rows in array that have common elements 2 110
File Find regex problem 4 68
Perl modules on linux ec2 3 123
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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 …
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…

730 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