Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

special RE variables

Posted on 2004-08-03
9
Medium Priority
?
213 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 800 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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 …
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…
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…
Six Sigma Control Plans

610 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