Regex problem.

Discussion in 'Perl Misc' started by Ash, Oct 10, 2007.

  1. Ash

    Ash Guest

    A B B c A c B d A A

    I have a string as represented above. They are separated by spaces of
    unknown length. I need to match As and Bs and store them in array in
    the order they occur, or as they occur. The array filled from above
    string should contain: ABBABAA.

    The split function on spaces cannot be used because A,B and Cs are
    representative of bigger string which may itself contain spaces for
    example A may represent "clever fox" and B may represent "jumps over"
    in which case the sequence ABB becomes "clever fox jumps over clever
    fox",so space cannot be used as delimiter.
    Global matching also cannot be used because it doesn't give me the
    order different pattern occur.

    Is there a way to solve this problem? Thank you.
     
    Ash, Oct 10, 2007
    #1
    1. Advertising

  2. On Wed, 10 Oct 2007 14:44:03 -0000, Ash <> wrote:

    >A B B c A c B d A A
    >
    >I have a string as represented above. They are separated by spaces of
    >unknown length. I need to match As and Bs and store them in array in
    >the order they occur, or as they occur. The array filled from above
    >string should contain: ABBABAA.


    my @AandBs = /[AB]/g;


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Oct 10, 2007
    #2
    1. Advertising

  3. On Oct 10, 10:52 am, Michele Dondi <> wrote:
    > On Wed, 10 Oct 2007 14:44:03 -0000, Ash <> wrote:
    > >A B B c A c B d A A

    >
    > >I have a string as represented above. They are separated by spaces of
    > >unknown length. I need to match As and Bs and store them in array in
    > >the order they occur, or as they occur. The array filled from above
    > >string should contain: ABBABAA.

    >
    > my @AandBs = /[AB]/g;


    That won't quite work since 'A' and 'B' are representative of more
    complex strings, and so can't fit into a character class.
     
    it_says_BALLS_on_your forehead, Oct 10, 2007
    #3
  4. Ash

    Ben Morrow Guest

    Quoth Ash <>:
    > A B B c A c B d A A
    >
    > I have a string as represented above. They are separated by spaces of
    > unknown length. I need to match As and Bs and store them in array in
    > the order they occur, or as they occur. The array filled from above
    > string should contain: ABBABAA.
    >
    > The split function on spaces cannot be used because A,B and Cs are
    > representative of bigger string which may itself contain spaces for
    > example A may represent "clever fox" and B may represent "jumps over"
    > in which case the sequence ABB becomes "clever fox jumps over clever
    > fox",so space cannot be used as delimiter.
    > Global matching also cannot be used because it doesn't give me the
    > order different pattern occur.


    Err... it does for me:

    ~% perl -le'$_ = "clever fox jumps over foo bar clever fox";
    print for /(clever fox|jumps over)/g'
    clever fox
    jumps over
    clever fox
    ~%

    Ben
     
    Ben Morrow, Oct 10, 2007
    #4
  5. Ash

    Mirco Wahab Guest

    Ash wrote:
    > A B B c A c B d A A
    >
    > I have a string as represented above. They are separated by spaces of
    > unknown length. I need to match As and Bs and store them in array in
    > the order they occur, or as they occur. The array filled from above
    > string should contain: ABBABAA.
    >
    > The split function on spaces cannot be used because A,B and Cs are
    > representative of bigger string which may itself contain spaces for
    > example A may represent "clever fox" and B may represent "jumps over"
    > in which case the sequence ABB becomes "clever fox jumps over clever
    > fox",so space cannot be used as delimiter.
    > Global matching also cannot be used because it doesn't give me the
    > order different pattern occur.


    I don't really understand your problem. *If* you have
    a string as you say above, then a simple:


    ...
    my $string='A B B c A c B d A A';
    my @array = $string=~/(?:\bA)|(?:\bB)/g;
    ...
    ...
    print "$_\n" for @array;
    ...


    should suffice. Whats meant by "doesn't give me the order different pattern occur"?

    Regards

    M.
     
    Mirco Wahab, Oct 10, 2007
    #5
  6. Ash

    Ash Guest

    Thanks for the replies guys, solution by Michele does work,
    apparently I didn't use "|" operator between regexs :(.
     
    Ash, Oct 10, 2007
    #6
  7. Ash

    Ash Guest

    Thanks for the replies guys, solution by Michele does work,
    apparently I didn't use "|" operator between regexs :(.
     
    Ash, Oct 10, 2007
    #7
  8. Ash

    Ash Guest

    Thanks for the replies guys, solution by Michele does work,
    apparently I didn't use "|" operator between regexs :(.
     
    Ash, Oct 10, 2007
    #8
  9. On Wed, 10 Oct 2007 14:55:22 -0000, it_says_BALLS_on_your forehead
    <> wrote:

    >> >A B B c A c B d A A

    >>
    >> >I have a string as represented above. They are separated by spaces of
    >> >unknown length. I need to match As and Bs and store them in array in
    >> >the order they occur, or as they occur. The array filled from above
    >> >string should contain: ABBABAA.

    >>
    >> my @AandBs = /[AB]/g;

    >
    >That won't quite work since 'A' and 'B' are representative of more
    >complex strings, and so can't fit into a character class.


    Then he should have said so. In fact I admit I was in a hurry and
    didn't read the rest of his post: somwhat my fault. But then one thing
    is to say that the problem is about

    A B B c A c B d A A

    and later specify that A is not A and B is not B and another thing is
    to say so to begin with. However if the problem is not more clearly
    defined, one can suppose that

    my @AandBs = /one|two or three/g;

    will be enough. If the OP wants to exclude a bone and two or
    threesome, then he may want to do

    my @AandBs = /\b(?:eek:ne|two or three)\b/g;

    If he wants something more complicated, then he should say so.


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Oct 10, 2007
    #9
  10. On Wed, 10 Oct 2007 16:56:20 -0000, Ash <> wrote:

    >Thanks for the replies guys, solution by Michele does work,
    >apparently I didn't use "|" operator between regexs :(.


    That is surprising, since my "solution" *didn't* include the
    alternation operator, at least not the one I had posted as of this
    writing.


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Oct 10, 2007
    #10
  11. Ash

    Mirco Wahab Guest

    Michele Dondi wrote:
    > On Wed, 10 Oct 2007 16:56:20 -0000, Ash <> wrote:
    >> Thanks for the replies guys, solution by Michele does work,
    >> apparently I didn't use "|" operator between regexs :(.

    >
    > That is surprising, since my "solution" *didn't* include the
    > alternation operator, at least not the one I had posted as of
    > this writing.


    His isp is advantagedata.com, which is (seen from Europe)
    behind .ALTER.NET, most probably located in the U.S.

    So this is probably a relativistic thing connected to earth
    rotation (his net requests are in earth-rotation direction).

    Regards

    M.
     
    Mirco Wahab, Oct 10, 2007
    #11
  12. On Wed, 10 Oct 2007 22:49:52 +0200, Mirco Wahab
    <-halle.de> wrote:

    >> That is surprising, since my "solution" *didn't* include the
    >> alternation operator, at least not the one I had posted as of
    >> this writing.

    >
    >His isp is advantagedata.com, which is (seen from Europe)
    >behind .ALTER.NET, most probably located in the U.S.
    >
    >So this is probably a relativistic thing connected to earth
    >rotation (his net requests are in earth-rotation direction).


    'LOL'.say;


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Oct 10, 2007
    #12
    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. =?Utf-8?B?SmViQnVzaGVsbA==?=

    Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine?

    =?Utf-8?B?SmViQnVzaGVsbA==?=, Oct 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    712
    =?Utf-8?B?SmViQnVzaGVsbA==?=
    Oct 22, 2005
  2. Rick Venter

    perl regex to java regex

    Rick Venter, Oct 29, 2003, in forum: Java
    Replies:
    5
    Views:
    1,635
    Ant...
    Nov 6, 2003
  3. Replies:
    2
    Views:
    601
  4. Xah Lee
    Replies:
    1
    Views:
    944
    Ilias Lazaridis
    Sep 22, 2006
  5. Replies:
    3
    Views:
    770
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page