Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Regular Expression Madness

1 view
Skip to first unread message

Michael Slater

unread,
Nov 15, 2001, 3:41:00 AM11/15/01
to
Hi..
I am having a heck of a time with a particular regular expression, that
should be very simple..

$text !~ s/<%.*%>//s;

what the above regex is supposed to do, is strip all asp tags and code out
of a block of html code.. but it does not do the job.

any ideas ?

regards,
Michael

Bernard El-Hagin

unread,
Nov 15, 2001, 4:04:08 AM11/15/01
to


Yup, use an HTML parser to parse HTML. I suggest HTML::Parser available
at http://www.cpan.org/


Cheers,
Bernard

Sean Hamilton

unread,
Nov 15, 2001, 4:10:57 AM11/15/01
to
> $text !~ s/<%.*%>//s;

Try:

$text =~ s/<%.*?%>//sg;

One of us is confused on the function of !~.

sh


Tyler Cruz

unread,
Nov 15, 2001, 5:03:51 AM11/15/01
to
I'm a Perl newbie so I'm not sure about this, but other than switching to
=~, doesn't the RegEx parameters have to be in parethethis?:

$text =~ s/<%(.*?)%>//sg;

Tyler Cruz

"Sean Hamilton" <s...@planetquake.com> wrote in message
news:BELI7.5108$c4.9...@news0.telusplanet.net...

Bart Lateur

unread,
Nov 15, 2001, 5:07:01 AM11/15/01
to
Michael Slater wrote:

Greediness. It will eat anything from the first opening marker till the
last closing marker. Try this instead:

$text =~ s/<%.*?%>//sg;

I'm assuming that the whole document is one string.

--
Bart.

Bernard El-Hagin

unread,
Nov 15, 2001, 5:07:54 AM11/15/01
to
On Thu, 15 Nov 2001 10:03:51 GMT, Tyler Cruz <tyle...@home.com> wrote:
> I'm a Perl newbie so I'm not sure about this, but other than switching to
>=~, doesn't the RegEx parameters have to be in parethethis?:
>
> $text =~ s/<%(.*?)%>//sg;


The parentheses are only necessary if you want to use what ".*?"
captures. It will be stored in the special $1 variable if the
()s are present. Read more in perlre.

But again, I have to warn you that you can't parse HTML with a regex.
Unless you're really sure of your input HTML you should be using
a parser. Don't say you weren't warned when you get burned.


Cheers,
Bernard

Paul Boardman

unread,
Nov 15, 2001, 6:15:09 AM11/15/01
to
Tyler Cruz wrote:
>
> I'm a Perl newbie so I'm not sure about this, but other than switching to
> =~, doesn't the RegEx parameters have to be in parethethis?:
>
> $text =~ s/<%(.*?)%>//sg;

Only if you want to store them for some reason.

Otherwise $text =~ s/<%.*?%>//sg; will work just fine (with all the
usual limitations/pitfalls on trying to use simple regexes to deal with
HTML style tags...).

Paul

Bart Lateur

unread,
Nov 15, 2001, 7:37:58 AM11/15/01
to
Bernard El-Hagin wrote:

>But again, I have to warn you that you can't parse HTML with a regex.
>Unless you're really sure of your input HTML you should be using
>a parser. Don't say you weren't warned when you get burned.

You'll get burned if you'll try parsing "<%...%>"-like tags with
HTML::Parser. It doesn't like it. Actually, it treats it like text
(splitting for calls between the "<" and the "%").

--
Bart.

Malcolm Dew-Jones

unread,
Nov 15, 2001, 1:37:19 PM11/15/01
to
Bernard El-Hagin (bernard....@lido-tech.net) wrote:

: On Thu, 15 Nov 2001 10:03:51 GMT, Tyler Cruz <tyle...@home.com> wrote:
: > I'm a Perl newbie so I'm not sure about this, but other than switching to
: >=~, doesn't the RegEx parameters have to be in parethethis?:
: >
: > $text =~ s/<%(.*?)%>//sg;


: The parentheses are only necessary if you want to use what ".*?"
: captures. It will be stored in the special $1 variable if the
: ()s are present. Read more in perlre.

Not entirely true, the ()'s are also important for logical grouping.

e.g. ([A-Z][a-z]*[0-9]+)?

the final ? is for everything in the parentheses - the match within the
()'s must happen exactly zero or one times.

Add "?:" to prevent the ()'s from setting $1 $2 etc.

e.g. (?:[A-Z][a-z]*[0-9]+)?

Bernard El-Hagin

unread,
Nov 16, 2001, 1:48:19 AM11/16/01
to
On 15 Nov 2001 10:37:19 -0800, Malcolm Dew-Jones <yf...@vtn1.victoria.tc.ca>
wrote:

> Bernard El-Hagin (bernard....@lido-tech.net) wrote:
>: On Thu, 15 Nov 2001 10:03:51 GMT, Tyler Cruz <tyle...@home.com> wrote:
>: > I'm a Perl newbie so I'm not sure about this, but other than switching to
>: >=~, doesn't the RegEx parameters have to be in parethethis?:
>: >
>: > $text =~ s/<%(.*?)%>//sg;
>
>
>: The parentheses are only necessary if you want to use what ".*?"
>: captures. It will be stored in the special $1 variable if the
>: ()s are present. Read more in perlre.
>
> Not entirely true, the ()'s are also important for logical grouping.


Entirely true in the example above. The OP asked about a specific
regex and I gave a specific response. I then pointed the OP at
the documentation using the words "read more" to suggest there's
more to read. When a beginner asks a specific question you shouldn't
complicate matters. Just give a specific answer and a pointer to more
information.


Cheers,
Bernard

Sachin Goyal

unread,
Dec 2, 2001, 4:22:32 PM12/2/01
to Michael Slater
Hello,

I am facing strange problem while using Perl Objects (aka modules). I am
not able to have any instace variable. If I make two objects of a class
and add some elements in one object, it automatically gets added in
second class also (as if it is singleton, but I do not want that). Any
ideas how to reslove that (Looks like i have not understood perl objects
clearly)

Any help will be welcome.

Here goes a simple program which can show my question

-- file check.pm
package check;

sub new {
my $ref = {};
bless $ref;
return $ref;
}

sub add {
my $ref = shift;
$ref{symbols} = {
"one" => "1",
"two" => "2"
};
}

sub print {
my $ref = shift;

foreach $name (sort keys %{$ref{symbols}}) {
print $ref{symbols}->{$name}."\n";

}

}

1;

-- file 1.pl
#!/usr/bin/perl -w

use check;

$oref = check->new();
$oref->add();
$oref->print();

print "Second Object\n";
$newref = check->new();

$newref->print();

---
Now if I run 1.pl

$ 1.pl
1
2
Second Object
1
2

So "one" and "two" is added automatically for both objects, even when I
have called "add" for only one object.

sachin

Sachin Goyal

unread,
Dec 2, 2001, 4:22:59 PM12/2/01
to Michael Slater

Sachin Goyal

unread,
Dec 2, 2001, 4:23:12 PM12/2/01
to Michael Slater

Sachin Goyal

unread,
Dec 2, 2001, 4:25:20 PM12/2/01
to Michael Slater

Sachin Goyal

unread,
Dec 2, 2001, 4:26:01 PM12/2/01
to Michael Slater

Damian Conway

unread,
Dec 2, 2001, 4:52:52 PM12/2/01
to
Sachin Goyal <sgo...@cs.utah.edu> writes:

> I am facing strange problem while using Perl Objects (aka modules). I am
> not able to have any instace variable. If I make two objects of a class
> and add some elements in one object, it automatically gets added in
> second class also (as if it is singleton, but I do not want that). Any
> ideas how to reslove that (Looks like i have not understood perl objects
> clearly)

Actually, it looks like understanding the correct syntax to access
via references is your problem.

> package check;
>
> sub new {
> my $ref = {};
> bless $ref;
> return $ref;
> }

So far, so good.


> sub add {
> my $ref = shift;
> $ref{symbols} = {

Here's the problem. That should be:

$ref->{symbols} = {

(You want the 'symbols' entry of the hash referred to by $ref,
*not* the 'symbols' entry of the hash %ref. Putting a 'use strict;'
at the start of your package would have pointed out this problem.)

> "one" => "1",
> "two" => "2"
> };
> }
>
> sub print {
> my $ref = shift;
>
> foreach $name (sort keys %{$ref{symbols}}) {
> print $ref{symbols}->{$name}."\n";

Same problem here. You want:

foreach $name (sort keys %{$ref->{symbols}}) {
print $ref->{symbols}->{$name}."\n";


Damian

Tassilo v. Parseval

unread,
Dec 2, 2001, 5:43:10 PM12/2/01
to
On 2 Dec 2001 21:52:52 GMT, Damian Conway wrote:
> Sachin Goyal <sgo...@cs.utah.edu> writes:

> Actually, it looks like understanding the correct syntax to access
> via references is your problem.
>
> > package check;
> >
> > sub new {
> > my $ref = {};
> > bless $ref;
> > return $ref;
> > }
>
> So far, so good.

Both syntactically and semantically the constructor is of course correct.
For futureness-'proofness' he might perhaps want to use the two-argument
form of bless in case he later wants to extend his classes and even add
a lot bit of inheritence to it. In the current form one couldn't
subclass 'check'.

sub new {
my $class = shift;
my $ref = bless {}, $class;
return $ref;
}

Regards,
Tassilo
--
Help fight continental drift.

Mina Naguib

unread,
Dec 2, 2001, 5:49:36 PM12/2/01
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

"Sachin Goyal" <sgo...@cs.utah.edu> wrote in message
news:3C0A9B98...@cs.utah.edu...


> Hello,
>
> I am facing strange problem while using Perl Objects (aka modules). I am
> not able to have any instace variable. If I make two objects of a class
> and add some elements in one object, it automatically gets added in
> second class also (as if it is singleton, but I do not want that). Any
> ideas how to reslove that (Looks like i have not understood perl objects
> clearly)
>
> Any help will be welcome.

Inside check.pm, when you receive $ref, you're treating it as a hash.

It's not a hash, it's a reference (AKA pointer) to a hash.

So to access the key "symbols", you can't do $ref{"symbols"} since that
overrides the original pointer with a new hash with the same name.

What you want is the dereferrence operator ->
It's used like so: $ref->{'symbols'}

Use that in your assignment and in your looping and you'll be fine :-)

-----BEGIN PGP SIGNATURE-----
Version: 6.5.8ckt http://www.ipgpp.com/
Comment: Think of PGP as a tamper-proof envelope for your email !
Comment: KeyID: 0x63227FAC
Comment: Fingerprint: A3C1 D7CF 4383 D614 74DA 54C9 792F 7DA4 6322 7FAC

iQA/AwUBPAqv/3kvfaRjIn+sEQKQSwCg2DkhmsuxIS3Z9IEj2nocdaVklK0AoJsj
HFxvb5SJUh0F+zfsawfRaJDy
=8KEo
-----END PGP SIGNATURE-----

Malcolm Dew-Jones

unread,
Dec 2, 2001, 6:32:41 PM12/2/01
to
Sachin Goyal (sgo...@cs.utah.edu) wrote:
: Hello,

: I am facing strange problem while using Perl Objects (aka modules). I am

(snip)
: Any help will be welcome.

Add "use strict" near the top.

: Here goes a simple program which can show my question

: -- file check.pm
: package check;

use strict; # ADDED

: sub new {


: my $ref = {};
: bless $ref;
: return $ref;
: }

: sub add {
: my $ref = shift;
: $ref{symbols} = {

The above error should be detected by the use strict.

: "one" => "1",
: "two" => "2"
: };
: }

: sub print {
: my $ref = shift;
:
: foreach $name (sort keys %{$ref{symbols}}) {
: print $ref{symbols}->{$name}."\n";

: }

: }

: 1;

(Please don't post your message so many times.)

0 new messages