Solved

special RE variables

Posted on 2004-08-03
9
196 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 

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

Suggested Solutions

Title # Comments Views Activity
Using Perl to parse rows 7 97
Replace  text in a file 2 114
Perl Frameworks 1 96
Call Shell Script from Perl Script 6 132
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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

791 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