Perl newbie regular expression usage question/help

Perl newbie regular expression usage question/help

am 11.11.2007 16:18:28 von dfairman16

Hi all

What I want my Perl programme to print out is

w
=
1
AND
(
(
x.y
=
"FRED ("
)
OR
(
z
=
2
)
)


generated from

w =1 AND ( (x.y="Fred (") OR (z=2) )

The problem is that although Perl and regular expressions are suitable
for the task, my starting point is that I have to learn Perl ... well
I'm learning it anyway, slowly, and the only thing that appears simple
is Hello World. Regular expressions are really complex, more than I
had thought. The bit the regular expression I am trying to use above
is that I don't care about spaces except for when they are enclosed by
double quotes (the "FRED (" bit in the query example above).


The query above is a real cut down simple version of the query string
that I would like to start to work on. The real query is a lot more
complex but I want to get it working for a very simple one first, then
extend the Perl, and during extending it learn more of the subset of
Perl I need to know to get it all done.

At the risk of being accused of asking someone to do my project for
me, as above is a starting point I would really appreciate being
pointed in the right direction. Not that it should matter but I
include the information for completeness, I am using Perl 5.8.8 on
Suse linux 8.

Thank you
David Fairman
UK


ps. I don't clear my email from this email account so reply in this
newsgroup. This email address is just one I use to stop me getting
spam at home.

Re: Perl newbie regular expression usage question/help

am 11.11.2007 16:31:25 von Samuel

On Sun, 11 Nov 2007 07:18:28 -0800, dfairman16 wrote:
> What I want my Perl programme to print out is
>
> w
> =
> 1
> AND
> (
> (
> x.y
> =
> "FRED ("
> )
> OR
> (
> z
> =
> 2
> )
> )
>
>
> generated from
>
> w =1 AND ( (x.y="Fred (") OR (z=2) )

You don't want to use plain regular expressions to do this, you want a
lexer/parser.

http://www.google.com/search?q=lex+yacc+perl

-Samuel

Re: Perl newbie regular expression usage question/help

am 11.11.2007 16:52:23 von dfairman16

On Nov 11, 3:31 pm, Samuel wrote:
> On Sun, 11 Nov 2007 07:18:28 -0800, dfairman16 wrote:
> > What I want my Perl programme to print out is
>
> > w
> > =
> > 1
> > AND
> > (
> > (
> > x.y
> > =
> > "FRED ("
> > )
> > OR
> > (
> > z
> > =
> > 2
> > )
> > )
>
> > generated from
>
> > w =1 AND ( (x.y="Fred (") OR (z=2) )
>
> You don't want to use plain regular expressions to do this, you want a
> lexer/parser.
>
> http://www.google.com/search?q=lex+yacc+perl
>
> -Samuel- Hide quoted text -
>
> - Show quoted text -

You are right, I do. I just didn't know this is what I wanted and
therefore didn't have the right terms to Google it out. Thank you very
much.

David

Re: Perl newbie regular expression usage question/help

am 11.11.2007 17:10:19 von Mark Clements

dfairman16@hotmail.com wrote:
> On Nov 11, 3:31 pm, Samuel wrote:
>> On Sun, 11 Nov 2007 07:18:28 -0800, dfairman16 wrote:
>>> What I want my Perl programme to print out is
>>> w
>>> =
>>> 1
>>> AND
>>> (
>>> (
>>> x.y
>>> =
>>> "FRED ("
>>> )
>>> OR
>>> (
>>> z
>>> =
>>> 2
>>> )
>>> )
>>> generated from
>>> w =1 AND ( (x.y="Fred (") OR (z=2) )
>> You don't want to use plain regular expressions to do this, you want a
>> lexer/parser.
>>
>> http://www.google.com/search?q=lex+yacc+perl
>>
>> -Samuel- Hide quoted text -
>>
>> - Show quoted text -
>
> You are right, I do. I just didn't know this is what I wanted and
> therefore didn't have the right terms to Google it out. Thank you very
> much.

You may have found this already but I found

http://www.perl.com/pub/a/2006/01/05/parsing.html?page=1

to be a good introduction.

Mark

Re: Perl newbie regular expression usage question/help

am 11.11.2007 17:26:58 von jurgenex

dfairman16@hotmail.com wrote:
> What I want my Perl programme to print out is
>
> w
> =
> 1
> AND
> (
> (
> x.y
> =
> "FRED ("
> )
> OR
> (
> z
> =
> 2
> )
> )
>
> generated from
>
> w =1 AND ( (x.y="Fred (") OR (z=2) )

It appears like you want to split the line at spaces:
split (/\s*/, 'w =1 AND ( (x.y="Fred (") OR (z=2)');
This is assuming that the non-split "FRED (" was an oversight in your sample
result above.

jue

Re: Perl newbie regular expression usage question/help

am 11.11.2007 17:43:13 von dfairman16

On Nov 11, 4:26 pm, "Jürgen Exner" wrote:
> dfairma...@hotmail.com wrote:
> > What I want my Perl programme to print out is
>
> > w
> > =3D
> > 1
> > AND
> > (
> > (
> > x.y
> > =3D
> > "FRED ("
> > )
> > OR
> > (
> > z
> > =3D
> > 2
> > )
> > )
>
> > generated from
>
> > w =3D1 AND ( (x.y=3D"Fred (") OR (z=3D2) )
>
> It appears like you want to split the line at spaces:
> split (/\s*/, 'w =3D1 AND ( (x.y=3D"Fred (") OR (z=3D2)');
> This is assuming that the non-split "FRED (" was an oversight in your sam=
ple
> result above.
>
> jue- Hide quoted text -
>
> - Show quoted text -


Thanks for your help Jürgen. I almost had something similar to what
you wrote in my Perl regular expression. The "FRED (" was not an
oversight however - the token I want is "FRED (" and including the
spaces. This is my big problem, spaces everythere except for between
quotes should be treated as just white space, in quotes they are not.

Thank you

David

Re: Perl newbie regular expression usage question/help

am 11.11.2007 17:50:11 von dfairman16

On Nov 11, 4:43 pm, dfairma...@hotmail.com wrote:
> On Nov 11, 4:26 pm, "Jürgen Exner" wrote:
>
>
>
>
>
> > dfairma...@hotmail.com wrote:
> > > What I want my Perl programme to print out is
>
> > > w
> > > =3D
> > > 1
> > > AND
> > > (
> > > (
> > > x.y
> > > =3D
> > > "FRED ("
> > > )
> > > OR
> > > (
> > > z
> > > =3D
> > > 2
> > > )
> > > )
>
> > > generated from
>
> > > w =3D1 AND ( (x.y=3D"Fred (") OR (z=3D2) )
>
> > It appears like you want to split the line at spaces:
> > split (/\s*/, 'w =3D1 AND ( (x.y=3D"Fred (") OR (z=3D2)');
> > This is assuming that the non-split "FRED (" was an oversight in your s=
ample
> > result above.
>
> > jue- Hide quoted text -
>
> > - Show quoted text -
>
> Thanks for your help Jürgen. I almost had something similar to what
> you wrote in my Perl regular expression. The "FRED (" was not an
> oversight however - the token I want is "FRED (" and including the
> spaces. This is my big problem, spaces everythere except for between
> quotes should be treated as just white space, in quotes they are not.
>
> Thank you
>
> David- Hide quoted text -
>
> - Show quoted text -

Oh, and the quotes should be part of the token too, ie. they shouldn't
be discarded.

Re: Perl newbie regular expression usage question/help

am 11.11.2007 18:18:34 von Tad McClellan

dfairman16@hotmail.com wrote:


> the token I want is "FRED (" and including the
> spaces. This is my big problem, spaces everythere except for between
> quotes should be treated as just white space, in quotes they are not.


Now you have a Question that is Asked Frequently:

How can I split a [character] delimited string except when
inside [character]?


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Re: Perl newbie regular expression usage question/help

am 11.11.2007 18:53:33 von dfairman16

On Nov 11, 5:18 pm, Tad McClellan wrote:
> dfairma...@hotmail.com wrote:
> > the token I want is "FRED (" and including the
> > spaces. This is my big problem, spaces everythere except for between
> > quotes should be treated as just white space, in quotes they are not.
>
> Now you have a Question that is Asked Frequently:
>
> How can I split a [character] delimited string except when
> inside [character]?
>
> --
> Tad McClellan
> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

OK, the link is

http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.28.html

I'm glad
1. You haven't come down on me with wrath
2. I mentioned out the outset I'm learning

Thank you
David.

Re: Perl newbie regular expression usage question/help

am 11.11.2007 19:35:23 von Gunnar Hjalmarsson

dfairman16@hotmail.com wrote:
> What I want my Perl programme to print out is
>
> w
> =
> 1
> AND
> (
> (
> x.y
> =
> "FRED ("
> )
> OR
> (
> z
> =
> 2
> )
> )
>
> generated from
>
> w =1 AND ( (x.y="Fred (") OR (z=2) )

local $_ = 'w =1 AND ( (x.y="Fred (") OR (z=2) )';
print "$_\n" for /".*?"|\w+\.\w+|\w+|\S/g;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Re: Perl newbie regular expression usage question/help

am 12.11.2007 02:39:46 von Tad McClellan

dfairman16@hotmail.com wrote:
> On Nov 11, 5:18 pm, Tad McClellan wrote:
>> dfairma...@hotmail.com wrote:
>> > the token I want is "FRED (" and including the
>> > spaces. This is my big problem, spaces everythere except for between
>> > quotes should be treated as just white space, in quotes they are not.
>>
>> Now you have a Question that is Asked Frequently:
>>
>> How can I split a [character] delimited string except when
>> inside [character]?
>>
>> --
>> Tad McClellan
>> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


[ it is poor netiquette to quote .sigs ]


> OK, the link is
>
> http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.28.html

> 2. I mentioned out the outset I'm learning


You do not need a link.

The Perl FAQ is installed on your own hard disk as part of
a normal perl install.

The docs (including the FAQ) installed with your perl will be
better than what you can find on the web, because it will be
specific to the version of perl that you are actually using.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"