-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of
Sandeep Deshpande
Sent: 21 September 2005 07:42
To: [EMAIL PROTECTED];
[email protected]
Subject: Regex query

Dear All,
This is a sample code, I hope it would explain my problem.
I want to extract some text from string based on some criteria.
1)      My definition
2)      Words
3)      Non-Words

My code goes as follows

===================================================================
$ref=" <bold> This a test <\/bold><med > ";

my $srch=qr(<bold>\ |<\/bold>|This\ a\ test)x;

while ($ref =~ s/((?:$srch)|\w+|\W+)//s) {
  my ($word) = ($1);
  print "#=>: $word#\n";
}
===================================================================

I get output like this, Since \W+ is more generic than $srch it captures
\W+
#=>:  <#
#=>: bold#
#=>: > #
#=>: This a test#
#=>:  </#
#=>: bold#
#=>: ><#
#=>: med#
#=>:  >
#

But I want output in the following way.

#=>:  #
#=>:<bold> #
#=>: This a test#
#=>: </bold>#
#=>: <#
#=>: med#
#=>:  >
#

My first comment would be that this looks remarkably like HTML/XML
markup. It is going to be difficult, if not impossible, to parse such
markup using regexes, unless it conforms to a pretty rigid format that
you can guarantee will never change. Much better to use a module that is
designed for the purpose, e.g HTML::Parser, XML::Parser).

Secondly, you are asking for trouble by having multiple alternatives
that can match the same string. The regex engine's idea of a "best"
match may not be the same as yours. You may be able to do it with a
single, more complicated, regex, but I would suggest using two regexes
in this case. See the following modification of your example. Note that
it is a self contained script that can be cut&paste and run, and that I
have removed a lot of unnecessary punctuation.

-------------------------------------------
use strict;
use warnings;

my $ref=" <bold> This a test </bold><med > ";

my $srch=qr(<bold> |</bold>|This a test);

while ($ref =~ s/($srch)//s or $ref =~ s/(\w+|\W+)//s) {
  my $word = $1;
  print "#=>: $word#\n";
}
-------------------------------------------

HTH

-- 
Brian Raven



=================================
Atos Euronext Market Solutions Disclaimer
=================================
The information contained in this e-mail is confidential and solely for the 
intended addressee(s). Unauthorised reproduction, disclosure, modification, 
and/or distribution of this email may be unlawful.
If you have received this email in error, please notify the sender immediately 
and delete it from your system. The views expressed in this message do not 
necessarily reflect those of Atos Euronext Market Solutions.

L'information contenue dans cet e-mail est confidentielle et uniquement 
destinee a la (aux) personnes a laquelle (auxquelle(s)) elle est adressee. 
Toute copie, publication ou diffusion de cet email est interdite. Si cet e-mail 
vous parvient par erreur, nous vous prions de bien vouloir prevenir 
l'expediteur immediatement et d'effacer le e-mail et annexes jointes de votre 
systeme. Le contenu de ce message electronique ne represente pas necessairement 
la position ou le point de vue d'Atos Euronext Market Solutions.


_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to