Can't get regex right

J

Jazeker

Hello,

Been playing around with a regex that should help a substitute of part
of an html template. I can't seem to get it right though. Anyone sees
what I am doing wrong ? I might have been staring at it for too long
myself...

subsitute : $tmpl_cont =~ s/\[\[LIST\]\](.*)\[\[\/LIST\]\]/$token/g;

(the $tmpl_cont var holds a join of the template - I have checked that
already by printing it to the browser and inspecting the page source.

template = http://www.galleryscript.net/demo/layout/xlisttemplate.html

As I was not sure about the wildcard, I also tried :

subsitute : $tmpl_cont =~ s/\[\[LIST\]\]([.\n]*)\[\[\/LIST\]\]/$token/g;

Thanks for any help...
 
G

Gunnar Hjalmarsson

Jazeker said:
Been playing around with a regex that should help a substitute of part
of an html template. I can't seem to get it right though. Anyone sees
what I am doing wrong ? I might have been staring at it for too long
myself...

subsitute : $tmpl_cont =~ s/\[\[LIST\]\](.*)\[\[\/LIST\]\]/$token/g;

You probably need the /s modifier. If there may be more than one
occurrence of the [
  • ] .. [
] pair, you should also make the
regex non-greedy, i.e. .*? . Otherwise the /g modifier is redundant.

perldoc perlre
 
J

Jazeker

Gunnar said:
Jazeker said:
Been playing around with a regex that should help a substitute of part
of an html template. I can't seem to get it right though. Anyone
sees what I am doing wrong ? I might have been staring at it for too
long myself...

subsitute : $tmpl_cont =~ s/\[\[LIST\]\](.*)\[\[\/LIST\]\]/$token/g;


You probably need the /s modifier. If there may be more than one
occurrence of the [
  • ] .. [
] pair, you should also make the
regex non-greedy, i.e. .*? . Otherwise the /g modifier is redundant.

perldoc perlre

Thank you so much ! I wonder then why ([\n.]*) did not do the trick...
 
J

Jazeker

Gunnar said:
Jazeker said:
Gunnar said:
You probably need the /s modifier.


Thank you so much ! I wonder then why ([\n.]*) did not do the trick...


Because the . character is not special within a character class. [\n.]
represents only a newline or a literal dot.
:-|

*starts beating himself very hard*
 
G

Gunnar Hjalmarsson

Jazeker said:
Gunnar said:
You probably need the /s modifier.

Thank you so much ! I wonder then why ([\n.]*) did not do the trick...

Because the . character is not special within a character class. [\n.]
represents only a newline or a literal dot.
 
T

Tad McClellan

Jazeker said:
Gunnar said:
Jazeker said:
Gunnar Hjalmarsson wrote:

You probably need the /s modifier.


Thank you so much ! I wonder then why ([\n.]*) did not do the trick...


Because the . character is not special within a character class. [\n.]
represents only a newline or a literal dot.
:-|

*starts beating himself very hard*


To avoid that in the future, let's review the only four characters
that are meta in a character class:

] ends the class, unless it is first

^ negates the class if it is first

- forms a range unless it is first or last

\ removes the meta-ness of any of the meta chars


That's it. Anything else is literal.
 
G

Gunnar Hjalmarsson

Tad said:
let's review the only four characters that are meta in a character
class:

] ends the class, unless it is first

^ negates the class if it is first

- forms a range unless it is first or last

\ removes the meta-ness of any of the meta chars


That's it. Anything else is literal.

It's not quite that easy, is it? E.g. \n and \t, as well as the \w and
\s character classes, retain their special meaning in a bracket
delimited character class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top