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

open(file, "apache.txt") ; @modify = <file> ; print @modify ; foreach(@modify){ chomp($_); $_ =~ s/^is$/are/g ; } print @modify ; print "this is new print" ;


In this code, I want to replace only 'is' (not 'is' in 'this') with 'are'. So I have created this code. But in the output 'is' did not get replaced with 'are'. Why is that. Please see output below.

Original file content is as follows
Apache is a modular server. This implies that only the most basic func +tionality is included in the core server. is it so ? Extended features are available through modules which can be loaded in +to Apache. By default, a base set of modules is included in the server at compile-time. If the server is compiled to u +se dynamic ally loaded modules, then modules can be compiled separately and added at any time using the LoadModule directi +ve. Otherw ise, Apache must be recompiled to add or remove modules. Configuration directives may be included conditional o +n a presen ce of a particular module by enclosing them in an <IfModule> block.

Output after execution
Apache is a modular server. This implies that only the most basic func +tionality is included in the core server. is it so ? Extended features are avail +able throu gh modules which can be loaded into Apache. By default, a base set of +modules is included in the server at compile-time. If the server is compiled to + use dynam ically loaded modules, then modules can be compiled separately and add +ed at any time using the LoadModule directive. Otherwise, Apache must be recompi +led to add or remove modules. Configuration directives may be included condition +al on a pr esence of a particular module by enclosing them in an <IfModule> block +. this is new print

Why am I not getting 'is' replaced by 'are' ?

Replies are listed 'Best First'.
Re: regex is not producing desired output
by ahmad (Hermit) on Jan 23, 2010 at 20:54 UTC

    Because you're telling the regex engine to match a line that contains the word 'is' only.

    ^ stands for a beginning of a line, while $ stands for the end of a line.

    so unless you have a line that contains only 'is' it won't match

    Your regex is better written as /\bis\b/are/g;

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: regex is not producing desired output
by ikegami (Patriarch) on Jan 23, 2010 at 20:55 UTC

    ^ = matches at start of string (and at start of a line with /m)
    $ = matches before a newline at end of string and at end of string (and before any newline with /m)
    \b = at a word boundary. That is to say, it matches between something that matches \w and something that matches \W in any order. The start and end of the string count as \W for this purpose.

    That is all in perlre

    open(my $fh, '<', "apache.txt") or die $!; while (<$fh>) { s/\bis\b/are/g; print; }
    or
    perl -pe"s/\bis\b/are/g" apache.txt
Re: regex is not producing desired output
by toolic (Bishop) on Jan 23, 2010 at 20:56 UTC
Re: regex is not producing desired output
by planetscape (Chancellor) on Jan 23, 2010 at 23:00 UTC
      thanks. I got how to do this one. I got another questio in the process. when I code "s/^is$/are/g" , this will look for 'is; in the beginning and at the end of string. So if I have
      <code> Apache is a modular server. This implies that only the most basic functionality is included in the core server. is it so ? Extended features are available through modules which can be loaded into Apache. By default, a base set of modules is included in the server at compile-time. If the server is compiled to use dynamically loaded modules, then modules can be compiled separately and added at any time using the LoadModule directive. Otherwise, Apache must be recompiled to add or remove modules. Configuration directives may be included conditional on a presence of a particular module by enclosing them in an <IfModule> block<code>
      is this will not change anything in the file.
      If I have
      Apache is a modular server. This implies that only the most basic functionality is included in the core server. is it so ? is Extended features are available through modules which can be loaded into Apache. By default, a base set of modules is included in the server at compile-time. If the server is compiled to use dynamically loaded modules, then modules can be compiled separately and added at any time using the LoadModule directive. Otherwise, Apache must be recompiled to add or remove modules. Configuration directives may be included conditional on a presence of a particular module by enclosing them in an <IfModule> block
      will this change is in the first line to are ? As its in the beginning and end of the string ?
      I tried it, but it did not change anything in the file

        I tried it, but it did not change anything in the file

        You didn't ask for help saving strings to a file, so I left that bit to you.

        perl -pe"s/\bis\b/are/g" apache.txt >apache.new

        Or here's a way that renames the original file for you and saves back to the original file:

        perl -i.bak -pe"s/\bis\b/are/g" apache.txt

        I'm afraid I do not understand your problem. If I take ikegami's code and modify it slightly so as not to depend on the existence of "apache.txt":

        while (<DATA>) { s/\bis\b/are/g; print; } __DATA__ Apache is a modular server. This implies that only the most basic func +tionality is included in the core server. is it so ? Extended features are available through modules which can be loaded in +to Apache. By default, a base set of modules is included in the server at compile-time. If the server is compiled to u +se dynamic ally loaded modules, then modules can be compiled separately and added at any time using the LoadModule directi +ve. Otherw ise, Apache must be recompiled to add or remove modules. Configuration directives may be included conditional o +n a presen ce of a particular module by enclosing them in an <IfModule> block.

        I get the following output (where clearly "is" has been replaced by "are" throughout):

        Apache are a modular server. This implies that only the most basic fun +ctionality are included in the core server. are it so ? Extended features are available through modules which can be loaded in +to Apache. By default, a base set of modules are included in the server at compile-time. If the server are compiled to +use dynami c ally loaded modules, then modules can be compiled separately and added at any time using the LoadModule directi +ve. Otherw ise, Apache must be recompiled to add or remove modules. Configuration directives may be included conditional o +n a presen ce of a particular module by enclosing them in an <IfModule> block.

        How does this differ from your desired output?

        HTH,

        planetscape

        I tried it, but it did not change anything in the file

        You didn't ask for help saving strings to a file, so I left that bit to you.

        perl -pe"s/\bis\b/are/g" apache.txt >apache.new

        Or here's a way that renames the original file for you and saves back to the original file:

        perl -i.bak -pe"s/\bis\b/are/g" apache.txt
        I tried it, but it did not change anything in the file

        ikegami's script the the correct way to do it. It doesn't change anything in the file, rather it opens the file handle for output only. If you want to modify the file, that's a totally different procedure and isn't pertinent to the question that you asked about the regex.