Regex problem.

A

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.

Is there a way to solve this problem? Thank you.
 
M

Michele Dondi

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
 
I

it_says_BALLS_on_your forehead

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.
 
B

Ben Morrow

Quoth Ash said:
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
 
M

Mirco Wahab

Ash said:
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.
 
A

Ash

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

Ash

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

Ash

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

Michele Dondi

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
 
M

Michele Dondi

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
 
M

Mirco Wahab

Michele said:
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.
 
M

Michele Dondi

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
 

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,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top