Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Regex problem.

Reply
Thread Tools

Regex problem.

 
 
Ash
Guest
Posts: n/a
 
      10-10-2007
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.

 
Reply With Quote
 
 
 
 
Michele Dondi
Guest
Posts: n/a
 
      10-10-2007
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,
 
Reply With Quote
 
 
 
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      10-10-2007
On Oct 10, 10:52 am, Michele Dondi <bik.m...@tiscalinet.it> wrote:
> On Wed, 10 Oct 2007 14:44:03 -0000, Ash <ashish...@gmail.com> 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.

 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      10-10-2007

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

 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      10-10-2007
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.
 
Reply With Quote
 
Ash
Guest
Posts: n/a
 
      10-10-2007
Thanks for the replies guys, solution by Michele does work,
apparently I didn't use "|" operator between regexs .

 
Reply With Quote
 
Ash
Guest
Posts: n/a
 
      10-10-2007
Thanks for the replies guys, solution by Michele does work,
apparently I didn't use "|" operator between regexs .

 
Reply With Quote
 
Ash
Guest
Posts: n/a
 
      10-10-2007
Thanks for the replies guys, solution by Michele does work,
apparently I didn't use "|" operator between regexs .

 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      10-10-2007
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(?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,
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      10-10-2007
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,
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
String Pattern Matching: regex and Python regex documentation Xah Lee Java 1 09-22-2006 07:11 PM
Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine? =?Utf-8?B?SmViQnVzaGVsbA==?= ASP .Net 2 10-22-2005 02:43 PM
Java regex imposture re: Perl regex compatibility a_c_Attlee@yahoo.com Java 2 05-06-2005 12:16 AM
perl regex to java regex Rick Venter Java 5 11-06-2003 10:55 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57