in reply to Re: regex is not producing desired output
in thread regex is not producing desired output

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
  • Comment on Re^2: regex is not producing desired output

Replies are listed 'Best First'.
Re^3: regex is not producing desired output
by ikegami (Patriarch) on Jan 26, 2010 at 06:15 UTC

    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
Re^3: regex is not producing desired output
by ikegami (Patriarch) on Jan 26, 2010 at 06:13 UTC

    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
Re^3: regex is not producing desired output
by planetscape (Chancellor) on Jan 25, 2010 at 07:48 UTC

    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
      Apache is a modular server. This implies that only the most basic functionality is included in the core server. is it so ?

      On this file , when I use following code
      open(file, "apache.txt") ; @modify = <file> ; print @modify ; foreach(@modify){ chomp($_); $_ =~ s/\bis\b/are/g ; # $_ =~ s/^is$/are/g ; } print @modify ;

      This will replace 'is" with 'are' which I tested correcty. Now when I comment out "$_ =~ s/\bis\b/are/g ;" and uncomment "$_ =~ s/^is$/are/g ;" ,'is' will not be replaced by 'are' , which was my original problem. As ^ will look at the beginning of string and $ will look at the end of string.

      If I add 'is' at the beginning and end of the string, will then 'is' be replaced with 'are' in the string ?

       is Apache is a modular server. This implies that only the most basic functionality is included in the core server. is it so ? is
      If I use regex "$_ =~ s/^is$/are/g ;" on this text, will it replace 'is' with 'are' as this string has 'is' at the beginning and end as well, which satisfies the regex condition.
      I tried, but it did not replace is with 'are'. So why did it not replace 'is' with 'are' ?
        You didn't ask to match an "is" at the start of the string or an "is" at the end of the string. You asked to match one that is both.
        >perl -le"$_='is'; s/^is$/are/g; print" are >perl -le"$_='is is is'; s/^is$/are/g; print" is is is >perl -le"$_='is is is'; s/^is|is$/are/g; print" are is are
Re^3: regex is not producing desired output
by Khen1950fx (Canon) on Jan 25, 2010 at 09:56 UTC
    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.

      Here's a way to modify a file in place:

      First, make a copy of your file and put it on your desktop. In this example, I called it apache.txt.
      Second, open a terminal.
      Third, from the cmdline, enter:

      perl -pi.orig -e 's/\bis\b/are/gi' /user/Desktop/apache.txt
      It'll modify the original file and create a new apache.txt. It'll save the original as apache.txt.orig.