Regex problem.

Regex problem.

am 10.10.2007 16:44:03 von ashishrai

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.

Re: Regex problem.

am 10.10.2007 16:52:05 von Michele Dondi

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^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: Regex problem.

am 10.10.2007 16:55:22 von it_says_BALLS_on_your forehead

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.

Re: Regex problem.

am 10.10.2007 17:13:55 von Ben Morrow

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

Re: Regex problem.

am 10.10.2007 17:23:49 von Mirco Wahab

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.

Re: Regex problem.

am 10.10.2007 18:56:20 von ashishrai

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

Re: Regex problem.

am 10.10.2007 18:56:58 von ashishrai

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

Re: Regex problem.

am 10.10.2007 19:31:48 von ashishrai

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

Re: Regex problem.

am 10.10.2007 21:59:20 von Michele Dondi

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(?:one|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^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: Regex problem.

am 10.10.2007 22:01:34 von Michele Dondi

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^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: Regex problem.

am 10.10.2007 22:49:52 von Mirco Wahab

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.

Re: Regex problem.

am 10.10.2007 23:41:02 von Michele Dondi

On Wed, 10 Oct 2007 22:49:52 +0200, Mirco Wahab
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^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: Regex problem.

am 12.10.2007 02:21:33 von sln

On Wed, 10 Oct 2007 21:59:20 +0200, Michele Dondi wrote:

>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(?:one|two or three)\b/g;
>
>If he wants something more complicated, then he should say so.

I don't want one two or three, I want everything else | nothing