in reply to Removing include files

I'm going to suggest this regex:

s|<!--#include virtual="/global_nav_[\w]+\.ssi"-->||g

which essentially replaces the matched value with nothing throughout the file (i used '|' as the regex delimeter to save ourselves from having to escape all the slashes inside the regex. also, append 'i' after the regex if you want your case insensitive matching again). This is also assuming there is ALWAYS another word at the end of "global_nav_" otherwise the [\w]+ should be a [\w]*.

Replies are listed 'Best First'.
Re: Re: Removing include files
by arturo (Vicar) on May 02, 2001 at 21:40 UTC

    The poster mentioned that the word at the end *may* be there, and it may not be; so probably you want that quantifier to be a * (zero or more) not a + (one or more).

    Also, underscores count as word characters (in the default locale on my systems (US English) at least), so that regex to get rid of those includes should be:

    s|<!--#include virtual="/global_nav\w*\.ssi)"-->||g;

    If you're not *sure* it's all *word* characters, then perhaps change that \w* to a [^.]*?, but that's probably unnecessary.

    However, I think those are the ones the poster wants to keep; all others are to be deleted. (UPDATE oh, that's just wrong. I reread the post. Sorry AidanLee ... but my question still stands).

    I don't have a neat solution for this. I tried

    s|<!--#include virtual="(?!/global_nav\w*\.ssi" -->||g;

    (using zero-width negative lookahead, i.e. "anything that doesn't match this pattern") but it didn't work, and I can't figure out how to fix it. Something's not working right in my brain today, but a two-step thing like this might suffice:

    foreach (@line_in body) { # quasi-pseudocode if (|(<!--#include virtual="([^"]+)"-->|) { #$1 contains the whole match, # $2 what's in between the quotes # so delete $1 unless $2 matches the pattern s/$1// unless $2 =~ m|/global_nav\w*\.ssi|; } }

    HTH ... and if anybody can tell me where my thinking's leading me down with my first stab, do tell!