Interaction between two strings

Discussion in 'Perl Misc' started by Ewout, Nov 10, 2003.

  1. Ewout

    Ewout Guest

    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
     
    Ewout, Nov 10, 2003
    #1
    1. Advertising

  2. Ewout

    Anno Siegel Guest

    Ewout <> 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
     
    Anno Siegel, Nov 10, 2003
    #2
    1. Advertising

  3. Ewout

    gilgames Guest

    <<
    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 =
     
    gilgames, Nov 10, 2003
    #3
  4. 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
     
    Gunnar Hjalmarsson, Nov 10, 2003
    #4
  5. Ewout

    Anno Siegel Guest

    gilgames <> 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
     
    Anno Siegel, Nov 10, 2003
    #5
  6. Ewout

    Tore Aursand Guest

    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 <>
     
    Tore Aursand, Nov 10, 2003
    #6
  7. 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
     
    Austin P. So (Hae Jin), Nov 11, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anita C

    Interaction between 2 webforms

    Anita C, Dec 4, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    317
    Munsifali Rashid
    Dec 5, 2003
  2. john
    Replies:
    2
    Views:
    489
    Chris Jackson
    Dec 11, 2003
  3. Denis Remezov

    Interaction between two programs

    Denis Remezov, May 20, 2004, in forum: C++
    Replies:
    3
    Views:
    383
  4. Michele Diegoli

    Interaction with two mouse

    Michele Diegoli, Feb 6, 2004, in forum: Python
    Replies:
    4
    Views:
    436
    Josiah Carlson
    Feb 7, 2004
  5. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    813
    Malcolm
    Jun 24, 2006
Loading...

Share This Page