regular expression question

Discussion in 'Perl Misc' started by Bart Van Loon, Jul 30, 2003.

  1. Hello,

    is it possible to make a regular expression match for the following
    situation:

    I have a string, looking like 'foobarbarbar'. I don't know what foo is,
    nor what bar is, the only thing I know is that I have a string X,
    concatenated an undefined number of times with a string Y. My goal is to
    find out how many times this string Y (bar) is repeted, without knowing
    what it exactly is.

    Something like ^.*(.*)*$ maybe?

    greetings,
    BBBart
     
    Bart Van Loon, Jul 30, 2003
    #1
    1. Advertising

  2. Bart Van Loon

    Greg Bacon Guest

    In article <UbNVa.749247$>,
    Bart Van Loon <> wrote:

    : I have a string, looking like 'foobarbarbar'. I don't know what foo
    : is, nor what bar is, the only thing I know is that I have a string X,
    : concatenated an undefined number of times with a string Y. My goal is
    : to find out how many times this string Y (bar) is repeted, without
    : knowing what it exactly is.

    % cat try
    #! /usr/local/bin/perl

    use warnings;
    use strict;

    my $X = 'foo';
    my $Y = 'bar';

    my $re = qr/^ $X ($Y)+ $/x;

    for (qw/ foo foobarbarbar foobar barbarfoo /) {
    if (/$re/) {
    print "Match for [$_]\n";
    }
    else {
    print "No match for [$_]\n";
    }
    }
    % ./try
    No match for [foo]
    Match for [foobarbarbar]
    Match for [foobar]
    No match for [barbarfoo]

    You may need to use quotemeta, depending on how you want to match.

    Hope this helps,
    Greg
    --
    I'm proud to be a taxpaying American, but I could be just as proud for
    half the money!
    -- Will Rogers
     
    Greg Bacon, Jul 30, 2003
    #2
    1. Advertising

  3. Bart Van Loon

    Sam Holden Guest

    On Wed, 30 Jul 2003 10:35:32 GMT, Bart Van Loon <> wrote:
    > Hello,
    >
    > is it possible to make a regular expression match for the following
    > situation:
    >
    > I have a string, looking like 'foobarbarbar'. I don't know what foo is,
    > nor what bar is, the only thing I know is that I have a string X,
    > concatenated an undefined number of times with a string Y. My goal is to
    > find out how many times this string Y (bar) is repeted, without knowing
    > what it exactly is.
    >
    > Something like ^.*(.*)*$ maybe?


    What's the point of a regular expression which matches all
    strings (assumming /s is used)?

    And do you use (.*)* because you want some extra added slowness?

    My naive approach would be to use something like (assumming "undefined
    number" means 2 or more - since otherwise it doesn't make sense):

    $_='foobarbarbar';
    if (/^(.+?)((.+)\3+)$/s) {
    print "X is $1\n";
    print "Y is $3\n";
    print "There are " . length($2)/length($3) . " repetitions of Y\n";
    }

    But that fails to match. However, /^(foo)((.+)\3+)$/ matches (and is what
    I tested the code with).

    Since (.+?) can match "foo", I think the first regular expression should
    match (after a fair bit of backtracking - guess the second .+ might be
    more efficient as .+?, but that's beside the point).

    echo "foobarbarbar" | egrep '^(.+)((.+)\3+)$'

    outputs "foobarbarbar" (I realise that \1 will be "foobar" and \2 "barbar"
    so the match is slightly different), so it seems to do what I would expect.

    Is this a bug in perl? Or is my knowedge of regexes faulty?

    I suspect the second, but (other than embaressment) it can't hurt to ask.

    --
    Sam Holden
     
    Sam Holden, Jul 30, 2003
    #3
  4. Bart Van Loon wrote at Wed, 30 Jul 2003 10:35:32 +0000:

    > is it possible to make a regular expression match for the following
    > situation:
    >
    > I have a string, looking like 'foobarbarbar'. I don't know what foo is,
    > nor what bar is, the only thing I know is that I have a string X,
    > concatenated an undefined number of times with a string Y. My goal is to
    > find out how many times this string Y (bar) is repeted, without knowing
    > what it exactly is.
    >
    > Something like ^.*(.*)*$ maybe?


    perl -e '$_ = "foobarbarbar"; /((.*)\2+)$/; print length($1)/length($2)'


    Greetings,
    Janek
     
    Janek Schleicher, Jul 30, 2003
    #4
  5. Bart Van Loon

    Jay Flaherty Guest

    Janek Schleicher wrote:

    > perl -e '$_ = "foobarbarbar"; /((.*)\2+)$/; print length($1)/length($2)'


    perl -e '$_ = "foobarbarbarbar"; /((.*)\2+)$/; print length($1)/length($2)'
    returns 2 (barbar x 2)
    So it seems to work with odd number of concats only.

    Jay
     
    Jay Flaherty, Jul 31, 2003
    #5
  6. Jay Flaherty wrote at Thu, 31 Jul 2003 13:14:50 -0400:

    > Janek Schleicher wrote:
    >
    >> perl -e '$_ = "foobarbarbar"; /((.*)\2+)$/; print length($1)/length($2)'

    >
    > perl -e '$_ = "foobarbarbarbar"; /((.*)\2+)$/; print length($1)/length($2)'
    > returns 2 (barbar x 2)
    > So it seems to work with odd number of concats only.


    No, it only returns prime numbers.
    In fact "barbarbarbar" is "barbar" x 2.
    And only prime number occurences can't be divided:

    But if it is meant that the shortest repeating part should be counted,
    you can use instead the non greedy version:

    perl -e '$_ = "foobarbarbarbar"; /((.*?)\2+)$/; print length($1)/length($2)'
    ^

    Greetings,
    Janek
     
    Janek Schleicher, Jul 31, 2003
    #6
    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. Andrew Munn

    Regular expression question...

    Andrew Munn, Jun 29, 2003, in forum: Perl
    Replies:
    1
    Views:
    2,136
    rakesh sharma
    Jun 30, 2003
  2. Glenn Kidd

    Regular expression question

    Glenn Kidd, Aug 18, 2003, in forum: Perl
    Replies:
    0
    Views:
    931
    Glenn Kidd
    Aug 18, 2003
  3. VSK
    Replies:
    2
    Views:
    2,303
  4. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    851
    Alan Moore
    Dec 2, 2005
  5. GIMME
    Replies:
    3
    Views:
    11,970
    vforvikash
    Dec 29, 2008
Loading...

Share This Page