Interaction between two strings

E

Ewout

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
 
A

Anno Siegel

Ewout said:
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
 
G

gilgames

<<
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 =
 
G

Gunnar Hjalmarsson

Ewout said:
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?
 
A

Anno Siegel

gilgames said:
<<
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
 
T

Tore Aursand

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
}
 
A

Austin P. So (Hae Jin)

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top