In a message dated 9/21/2005 3:15:58 A.M. Eastern Standard Time, [EMAIL PROTECTED] writes:
 
> 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#
> #=>:  >
> #
>
>
> Regards,
> Sandeep Deshpande
sandeep --  
 
use strict;
use warnings;
 
my $r = qq( <bold> This a test </bold><med >\n);
my $s = qr(<bold> |</bold>|This a test);
while ($r =~ m/($s|^\s+|\w+|\s+[^\s\w]+\s+|[^\s\w])/g) {
   my $w = $1; print qq('$w'\n)
   }
 
produces:  
' '
'<bold> '
'This a test'
'</bold>'
'<'
'med'
' >
'
 
as far as i can tell, the output is EXACTLY as specified in your query (reading whitespace in e-mail is a bit tricky).   i have taken the liberty of stripping off what i consider to be extraneous punctuation, escapes, switches, etc.   i also have used a m// match instead of a s/// substitution since it did not seem that the substitution was functional (all you are left with, i think, is a single space) and because i couldn't figure out how to do it with a substitution (that pesky leading space causes trouble).  
 
the trick is to catch the space(s) at the start of the test string, ignore the space(s) embedded in the string between ``test'' and ``<'', and catch the ``<'' before ``med'' on its own.   this regex is VERY FRAGILE, and i would heed brian's advice to look seriously at using an HTML/XML parser.  
 
hth -- bill walters  
 
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to