in reply to Re^3: Reg exp questions
in thread Reg exp questions

my $str = 'dfsldjf(djfls)jfslfk kfds\nfjsldf';

output: dfsldjf_djfls_jfslfk_kfds fjsldf

Replies are listed 'Best First'.
Re^5: Reg exp questions
by toolic (Bishop) on Nov 07, 2014 at 19:48 UTC
    Did you really mean to include a newline character (single quotes prevent interpolation)?
    use warnings; use strict; my $str = 'dfsldjf(djfls)jfslfk kfds\nfjsldf'; $str =~ s/[\s()]/_/g; $str =~ s/\\n/ /g; print "$str\n"; __END__ dfsldjf_djfls_jfslfk_kfds fjsldf

      well I have a file a lines and there is \n between lines. In order to send an example of what I have in one string, I included \n in the string. Otherwise, it's like

      HeadeRdfsldjf(djfls)jfslfk kfd

      lfjslfjsflk

      dkflskfs

      HeadeRRdfsldjf(djfls)jfslfk kfd

      lfjslfjsflk

      dkflskfs

      in the header line, ()\s should be replaced by _, \n at the end should be replaced by \s, on the following lines without Header, \n should be removed except the last one so that it won't be concatenated with the next header line:

      HeadeRdfsldjf_djfls_jfslfk_kfd lfjslfjsflkdkflskfs

      HeadeRdfsldjf_djfls_jfslfk_kfd lfjslfjsflkdkflskfs

      =~ s/\\n/ /g; doesn't work for me. I don't get space instead of \n.

        \s doesn't mean 'space', it means 'ascii/unicode whitespace characters', which include, among others, newline, tab, space.

      So finally, $str =~ s/\n/ /g; removes \n. Does it remove EOF, too? If so, how to avoid?

        So finally, $str =~ s/\n/ /g; removes \n. Does it remove EOF, too?

        removes \n ... replaces \n by a blank.

        And no, it has no effect on the EOF character.

        Cheers, Sören

        Créateur des bugs mobiles - let loose once, run everywhere.
        (hooked on the Perl Programming language)