kepler has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

In fact I never understood very well this part of the regex: please supose I have these 3 different lines in array for instance:

1) func1(parameter1,"ok...", var1)

2)#define "this is my name"

3) "hi"

I want to substitute the data between strings in different manner for each case. There are several almost common cases. If I create a regex for the first, how can I create a second, for the 2nd line and 3th, excluding the word function func1, for instance? The exclusion of an entire word, not just characters, all ways made me confused. And it might not be at the beggining of a sentence. I saw solutions like (?!word) that don't work... Any guidance would be apreciated...

Kepler

Replies are listed 'Best First'.
Re: String replace - Part II
by hippo (Archbishop) on Sep 21, 2016 at 09:54 UTC
    I saw solutions like (?!word) that don't work

    I'm afraid that really isn't a useful description of the problem. Please provide an SSCCE to demonstrate, include your O/S, version of perl, and full text of the error message you receive. Better yet, provide the code as a test as explained in How to ask better questions using Test::More and sample data and the readers will have a much better chance of explaining how to solve it.

      Ok. Will do. Thanks.
Re: String replace - Part II
by AnomalousMonk (Archbishop) on Sep 21, 2016 at 12:47 UTC

    Like hippo, I don't really understand the questions(s) you're asking in the OP, and I agree with his or her recommendations here. That said, I can hazard an answer based on part of your post:

    The exclusion of an entire word, not just characters ...

    I think what you may be asking for is an alternative to  [^somechars] (an inverted character class) in an  [^somechars]* expression that will generalize to any pattern. Something like this may fill the bill:

    c:\@Work\Perl\monks>perl -wMstrict -le "my $pattern = qr{ \b f[eio]e \b }xms; ;; my $str = 'foo bar feeble macfee unfeeling fee fumble'; ;; print qq{match: '$1'} if $str =~ m{ (bar (?: (?! $pattern) .)* f \w+) + }xms; " match: 'bar feeble macfee unfeeling fee'
    The  (?: (?! $pattern) .)* sub-pattern matches "zero or more of any character so long as the character is not at the start of something matching $pattern." Contrast with the behavior of
        m{ (bar .* f \w+) }xms
    (Update: Also contrast with  m{ (bar (?: (?! $pattern) .)*? f \w+) }xms (note  ? "lazy" quantifier modifier).)

    Update: As a parenthetic note, the example above can be extended to show regex composition:

    c:\@Work\Perl\monks>perl -wMstrict -le "my $giant_sez = qr{ \b f[eio]e \b }xms; my $not_giant = qr{ (?! $giant_sez) . }xms; ;; my $str = 'foo bar feeble macfee unfeeling fee fumble'; ;; print qq{match: '$1'} if $str =~ m{ (bar $not_giant* f \w+) }xms; " match: 'bar feeble macfee unfeeling fee'
    Again, contrast with  m{ (bar $not_giant*? f \w+) }xms


    Give a man a fish:  <%-{-{-{-<

Re: String replace - Part II
by tybalt89 (Monsignor) on Sep 21, 2016 at 16:11 UTC

    Is this the kind of thing you are looking for?

    #!/usr/bin/perl -l # http://perlmonks.org/?node_id=1172291 use strict; use warnings; my @array = <DATA>; print @array; /^\w/ ? s/"(.*?)"/"type 1 - $1"/g : /^#/ ? s/"(.*?)"/"type 2 - $1"/g : s/"(.*?)"/"type 3 - $1"/g for @array; print @array; __DATA__ func1(parameter1,"ok...", var1) #define "this is my name" "hi" anotherfunc(foo) "string in quotes" #define "more" #define "still more" morefuncs("foo", bar, "baz")
Re: String replace - Part II
by Anonymous Monk on Sep 21, 2016 at 15:29 UTC
    Why do you need to do this all with regexes? Can you do something like this?
    if (/func1/) { s/blah/blah/ } elsif (/^#define/) { s/yak/yak/ } else { s/mumble/mumble/ }
    Your examples look like C code. If your requirements are very complex, you may need to use a more advanced parser design -- parsing C with just a regex is not possible.