Solved

special RE variables

Posted on 2004-08-03
9
169 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
Comment Utility
@matches = "abFFacdABCfac" =~ /ab(.*)acd(.*)fac/;
print map"! - $_\n",@matches;
0
 

Author Comment

by:ole111
Comment Utility
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
Comment Utility
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
 

Author Comment

by:ole111
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 20

Accepted Solution

by:
jmcg earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
that's ok, i found it myself. thnx
0
 

Author Comment

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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

10 Experts available now in Live!

Get 1:1 Help Now