Re: String Manipulation



Thank you. But i am unable to understand the working of the code which you
have written. Can you please explain it?

Thanks and Regards,
Dharshana

On 6/28/07, Chas Owens <chas.owens@xxxxxxxxx> wrote:

On 6/27/07, Dharshana Eswaran <dharshana.ve@xxxxxxxxx> wrote:
> On 6/28/07, Tom Phoenix <tom@xxxxxxxxxxxxxx> wrote:
> >
> > On 6/27/07, Dharshana Eswaran <dharshana.ve@xxxxxxxxx> wrote:
> >
> > > I am unable to get a generalised way in which it can extract them as
few
> > > structures have comments, few does not hav comments etc.
> >
> > Does the data have some defined grammar, or a definable one at least?
>
>
>
> The defined Grammer here is
> {
> xyz1 abc1; /*Comments*/
> xyz2 abc2;
> xyz3 abc3[req];
> xyz4 abc4[req]; /*Comments*/
> };
>
> Here, i have defined different possibility of occurences of the
structure
> elements. If i could get a regex for extracting xyz1, xyz2, xyz3, xyz4
and
> abc1, abc2, abc3[req], abc4[req] would be helpful. Here, the comments
are of
> no use, i just need to ignore them.
>
> >If you are up to using Parse::RecDescent, it will probably do the job.
>
> I am restricted from using modules and i am unable to come up with a
regex
> or regexes to do this job.
>
> >http://search.cpan.org/author/DCONWAY/Parse-RecDescent-1.94
> >/lib/Parse/RecDescent.pod
>
> >Hope this helps!
>
> >--Tom Phoenix
> >Stonehenge Perl Training
>
> Can anyone guide me in this?
>
> Thanks and Regards,
> Dharshana
>

It is fragile, but here are a set of regexes that parse the string you
mentioned. I did notice that this string differs significantly from
the ones you gave earlier and this set of regexes will not correctly
handle them.

#!/usr/bin/perl

use strict;
use warnings;

my $comment = qr{\s* (?:/\* .*? \*/ \s*)*}xs;
my $identifier = qr{ [A-Za-z_]\w* }xs;
my $statement = qr{
\s*
($identifier)
\s+
($identifier)
\s*
(?: \[ (.*?) \] )?
\s*
;
\s*
$comment?
}xs;

my $str = <<EOS;
{
xyz1 abc1; /*Comments*/
xyz2 abc2;
xyz3 abc3[req];
xyz4 abc4[req]; /*Comments*/
};
EOS

my @m = $str =~ /$statement/g;

my $iter = by_n(3, \@m);

while ((my ($type, $var, $elems) = $iter->()) == 3) {
if ($elems) {
$type = "array of $type with $elems elements";
}
print "type is $type and variable is $var\n";
}

sub by_n {
my ($n, $a) = @_;
my $i = 0;
sub {
return undef if $i > $#$a;
my @ret = @{$a}[$i .. $i + $n - 1];
$i += $n;
return @ret;
}
}



Relevant Pages

  • Re: String Manipulation
    ... On 6/27/07, Dharshana Eswaran wrote: ... >> I am unable to get a generalised way in which it can extract them as few ... or regexes to do this job. ... sub by_n { ...
    (perl.beginners)
  • Re: Module reuse
    ... >> regex syntax. ... yet learned the basics about regexes. ... to write the regex correctly, if you choose to take that route. ... Gunnar Hjalmarsson ...
    (perl.beginners)
  • Re: Program inefficiency?
    ... Even regexes can be legible enough when they are well documented, not to mention the fact that is an idiom common to various languages. ... you spent some time interpreting his code. ... It still doesn't solve the problem of one tag having both attributes with spaces (which can be easily fixed with a second regex, but that was out of question:P), and there can be a lot of other problems, but should provide at least some guidance. ...
    (comp.lang.python)
  • Re: [geek] Advanced Siteswap RegEx
    ... > I've recently been playing around with regexes, ... > It did strike me that with lookaheads and backreferences, ... > regex engine itself. ... > As I've mostly been playing with lookaheads and backreferences at work ...
    (rec.juggling)
  • Re: Need Regular Expressions?
    ... I'm perfectly happy with regexes in a production situation, ... It's also worth noting that the .Net framework includes a regex engine ... (whose regexes are a superset of those available in VBScript). ... Likewise the O'Reilly Regular Expression Pocket Reference. ...
    (microsoft.public.access.modulesdaovba)