in reply to Re^2: Substitution in range operator
in thread Substitution in range operator

Hm, odd. Please try running this program:

#!/usr/bin/perl use strict; use warnings; local $/; my $txt = <DATA>; $txt =~ s/\n\|/ /g; print $txt; __DATA__ foo | bar baz |qux fred |wilma barney
Output should be :
foo | bar baz qux fred wilma barney
(Note that you don't need to enclose your desired match in parentheses unless you need to "capture" it.)


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^4: Substitution in range operator
by benaw (Novice) on Dec 30, 2015 at 06:35 UTC

    Output:

    foo | bar baz qux fred wilma barney

    Perl -v perl 5, version 14 subversion 2 (v5.14.2) built for cygwin-thread-multi-64int

      I'm not sure if it is an issue with the Perl version (I am using 5.16.3 and 5.22), but rather with line endings on Windows. I don't use Windows myself, but there are considerations with line-endings on that platform; do a search and you'll find at least a couple of discussions about the topic in the last couple of months.

      What happens if you change the key line to:

      $txt =~ s/\r\n\|/ /g;
      ?


      The way forward always starts with a minimal test.

        I get the correct output but in my script it doesn't change the pipe or remove the new line. Funny because i copied the char into a hex editor and got "a" (new line not carriage return) I'll play around with it some more tomorrow but is there a way to use hex values in the regex? - just googled http://www.regular-expressions.info/nonprint.html so I'll have another look at a hex editor tomorrow and give it a go trying with hex symbols.

        foo | bar baz qux fred wilma barney

        I get the correct output but in my script it doesn't change the pipe or remove the new line. Funny because i copied the char into a hex editor and got "a" (new line not carriage return) I'll play around with it some more tomorrow but is there a way to use hex values in the regex? - just googled http://www.regular-expressions.info/nonprint.html so I'll have another look at a hex editor tomorrow and give it a go trying with hex symbols.

        foo | bar baz qux fred wilma barney