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

Hi, I have a question regarding Perl's regular expressions. I have the string:
$str = "Availability: Immediate.\n Title: The quantitative properties of log \n normal distributions.\n Author: John S. Peters\n";
and I need to write a regular expression to remove the newlines \n from the contents of the Title only (not the ones before or after the value of Title). I have been experimenting with:

$str =~ s/(Title)(.*?)\n(.*?)(Author)/$1$2$3$4/g;

but it does not seem to work. Is there a way to make it work without using a while() loop? Also, is there a graphical debugger for Perl's regular expressions?

Thanks,
Chris

Replies are listed 'Best First'.
Re: Question on REs - Graphical debugger?
by boo_radley (Parson) on May 23, 2002 at 18:13 UTC
    You may wish to see 157925, wherein the same topic is discussed.
    If you're willing to install wxperl : <shill type="self">164418</shill>
      Personally I like to use Vim with ":set hls" and ":set incsearch", and the standard Vim search/replace function to interactively test my regexp on the text in the buffer, then I convert the "vim regexp" to a "perl regexp" ;)
Re: Question on REs - Graphical debugger?
by amarceluk (Beadle) on May 23, 2002 at 18:28 UTC
    You need the s modifier to allow .* to match a newline character:
    $str =~ s/(Title)(.*?)\n(.*?)(Author)/$1$2$3$4/gs;