Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Interaction between two strings

Reply
Thread Tools

Interaction between two strings

 
 
Ewout
Guest
Posts: n/a
 
      11-10-2003
In a regex, I want to use some characters from a string that is
supplied as a kind of parameter value. I will give an example to
explain this.

Assume that the parameter value is ABCR. The regex should be such that
for example a string like ABCC should give a match. But if the
parameter value is XYZR, then the string XYZC should give a match. To
summarize: the regex should take part of the pattern from another
string that is supplied separately from the regex. Is there any way to
achieve this?

I have been thinking about the possibility to do some kind of
preprocessing on the regex pattern. For example, I might state that a
string pattern like **2** will take the character on the second
position of the parameter string. In the above example, the string
pattern would be **1****2****3**C (or maybe **1,2,3**C). After doing
the necessary substitutions, I would then present the resulting
(regular!) regex pattern to a regex compiler. Would this be a sound
approach?

For your information: I am programming in ILE RPG on an iSeries
system. I am not at all familiar with Perl etcetera.

Thanks in advance.

Ewout
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      11-10-2003
Ewout <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> In a regex, I want to use some characters from a string that is
> supplied as a kind of parameter value. I will give an example to
> explain this.
>
> Assume that the parameter value is ABCR. The regex should be such that
> for example a string like ABCC should give a match. But if the
> parameter value is XYZR, then the string XYZC should give a match. To
> summarize: the regex should take part of the pattern from another
> string that is supplied separately from the regex. Is there any way to
> achieve this?


Achieve what?

Your description doesn't show the relationship between the parameter
values and the desired matches. Giving a single example for two cases
is not enough. Apparently, the first three characters of the parameter
have to match literally. What about the fourth one?

> I have been thinking about the possibility to do some kind of
> preprocessing on the regex pattern. For example, I might state that a
> string pattern like **2** will take the character on the second
> position of the parameter string. In the above example, the string
> pattern would be **1****2****3**C (or maybe **1,2,3**C). After doing
> the necessary substitutions, I would then present the resulting
> (regular!) regex pattern to a regex compiler. Would this be a sound
> approach?


That's impossible to answer before you have specified your problem.

You may want a table approach. Build a hash that holds the possible
parameter values as keys and the corresponding regular expressions
as values. Example:

my %regtab = (
ABCR => qr/ABC./,
XYZR => qr/XYZ./,
);

This would fit your examples, but so would a million others.

Anno
 
Reply With Quote
 
 
 
 
gilgames
Guest
Posts: n/a
 
      11-10-2003
<<
In a regex, I want to use some characters from a string that is
supplied as a kind of parameter value. I will give an example to
explain this.
>>




$pattern='abcd';
$string='abcdaaayz';

if ($string =~ /($pattern)/) {print "Match: ";}
else {print "Nomatch: ";}

print "$string =~ $pattern = $1\n";

prints :

Match: abcdaaayz =~ abcd = abcd


The same with $pattern = 'xyz' prints:

Nomatch: abcdaaayz =~ xyz =

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      11-10-2003
Ewout wrote:
> In a regex, I want to use some characters from a string that is
> supplied as a kind of parameter value. I will give an example to
> explain this.
>
> Assume that the parameter value is ABCR. The regex should be such
> that for example a string like ABCC should give a match. But if the
> parameter value is XYZR, then the string XYZC should give a match.
> To summarize: the regex should take part of the pattern from
> another string that is supplied separately from the regex. Is there
> any way to achieve this?


This is one way in Perl that fits your examples:

print "Match\n" if $string =~ /^${\substr $param, 0, 3}/;

> I have been thinking about the possibility to do some kind of
> preprocessing on the regex pattern. For example, I might state that
> a string pattern like **2** will take the character on the second
> position of the parameter string. In the above example, the string
> pattern would be **1****2****3**C (or maybe **1,2,3**C). After
> doing the necessary substitutions, I would then present the
> resulting (regular!) regex pattern to a regex compiler. Would this
> be a sound approach?


It's probably sound to make the preprocessing outside the regex.

> For your information: I am programming in ILE RPG on an iSeries
> system. I am not at all familiar with Perl etcetera.


Then why do you ask for help here?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      11-10-2003
gilgames <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> <<
> In a regex, I want to use some characters from a string that is
> supplied as a kind of parameter value. I will give an example to
> explain this.
> >>

>
>
>
> $pattern='abcd';
> $string='abcdaaayz';
>
> if ($string =~ /($pattern)/) {print "Match: ";}
> else {print "Nomatch: ";}
>
> print "$string =~ $pattern = $1\n";
>
> prints :
>
> Match: abcdaaayz =~ abcd = abcd
>
>
> The same with $pattern = 'xyz' prints:
>
> Nomatch: abcdaaayz =~ xyz =


I still don't get it. Yes, the code above would behave as you describe.
Now, what characters from what string do you want to use as a parameter
value in which way?

You really got to work on the way you present your problem.

Anno
 
Reply With Quote
 
Tore Aursand
Guest
Posts: n/a
 
      11-10-2003
On Mon, 10 Nov 2003 04:52:35 -0800, Ewout wrote:
> Assume that the parameter value is ABCR. The regex should be such that
> for example a string like ABCC should give a match. But if the
> parameter value is XYZR, then the string XYZC should give a match. To
> summarize: the regex should take part of the pattern from another
> string that is supplied separately from the regex. Is there any way to
> achieve this?


Sounds to me like substr() would cover your needs pretty well;

if ( substr($s1,0,3) eq substr($s2,0,3) ) {
# Match
}


--
Tore Aursand <(E-Mail Removed)>
 
Reply With Quote
 
Austin P. So (Hae Jin)
Guest
Posts: n/a
 
      11-11-2003


Ewout wrote:

> Assume that the parameter value is ABCR. The regex should be such that
> for example a string like ABCC should give a match. But if the
> parameter value is XYZR, then the string XYZC should give a match. To
> summarize: the regex should take part of the pattern from another
> string that is supplied separately from the regex. Is there any way to
> achieve this?


I'm a crappy perl programmer, but I think I understand your problem
despite the fact that you are being very ambiguous about it.

You seem to be doing what many "bioinformaticians" have been trying to
do, which is compare two sequences and find patterns of similarity
between these two sequences....so you might want to look in the bioperl
module (www.bioperl.org).

If you do as you wish and "parameterize" it, you will have to find a
metric and a weighting method which makes it a hard problem...

Otherwise, you could do what I think you are thinking of doing, and you
could generate a set of wild card matches which you could use to regex.
There are obvious memory issues (x**n permutations of x characters of
length n), but one trick is rather than build an array or hash of
$patterns, you splice them all into one very big $pattern.

i.e. $pattern = "x1|x2|x3....|xn";
$string =~ /$pattern/;

Doing this is immensely faster than iterating through an array or the like:

i.e. foreach $pattern (@pattern){
$string =~ /$pattern/;


This was mentioned somewhere...Perl Cookbook? Or it may have been the
advanced book of Perl Programming...

Austin


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
Interaction between two programs Denis Remezov C++ 3 05-20-2004 09:06 PM
Interaction with two mouse Michele Diegoli Python 4 02-07-2004 04:37 AM
Interaction between 2 different web browsers john ASP .Net 2 12-11-2003 07:43 PM
Interaction between 2 webforms Anita C ASP .Net 1 12-05-2003 12:53 PM



Advertisments