Page 1 of 1

Dealing with Carriage Returns/New Lines

Posted: 16 Jun 2015 20:46
by SkyFrontier
How do I regexreplace ^M and ^P special characters (possible others in the likes...?)?

Reference:
http://stackoverflow.com/questions/8111 ... ned-in-vim
http://www.ultraedit.com/support/tutori ... sions.html (scroll down to ^P entry)

Thanks in advance.

Re: Dealing with Carriage Returns/New Lines

Posted: 16 Jun 2015 21:49
by highend
Did you try "\r?\n"?

Re: Dealing with Carriage Returns/New Lines

Posted: 16 Jun 2015 22:43
by SkyFrontier
Yes.
Regex engine seems to simply ignore those ones.
Thanks for the feedback.

Re: Dealing with Carriage Returns/New Lines

Posted: 16 Jun 2015 23:00
by highend
It's definitely not the most advanced one (vbscript.dll)...

Can you provide a test file?

Re: Dealing with Carriage Returns/New Lines

Posted: 16 Jun 2015 23:14
by SkyFrontier
Sure.
Attached.

Re: Dealing with Carriage Returns/New Lines

Posted: 16 Jun 2015 23:19
by highend
Thanks.

Mh, I don't know if I get it.
You just want to replace "^P" and "^M" (the characters) with a linefeed?

It can't be that easy, right?

If it's really the case, nothing easier than that...

Code: Select all

$a = <<<>>>
^P - "
  " >> this is the base value and variants like "
" or "
  ";
^M - 
* Home
*| About
*|
>>>;

    text regexreplace($a, "(\^P|\^M)", "<crlf>", 1);

Re: Dealing with Carriage Returns/New Lines

Posted: 16 Jun 2015 23:27
by SkyFrontier
^Ps are still there (visible using VIM), while ^Ms do a line feed when they shouldn't (I replaced "<crlf>" by "" as I want to erase them from the input).

PS: when you open the sample, you may see (notepad) one liners for both samples.

Re: Dealing with Carriage Returns/New Lines

Posted: 16 Jun 2015 23:54
by highend
Ok, I see. Try this:

Code: Select all

$a = readfile("D:\NULLcharactersSample_2015.xys");
    writefile("NULLcharactersSample_2015-1.xys", regexreplace($a, "(\^P|\^M|\x0A|\x10|\x0D)", "", 1));

Re: Dealing with Carriage Returns/New Lines

Posted: 17 Jun 2015 00:40
by SkyFrontier
Hi.
Thanks - now I can remove the ^M just using \x0D, but when trying to remove ^P (using several combinations/order of items), it wipes <crlf> - which is not desired.
I see ^P signals a paragraph, but how to differentiate them from needed <crlf>?

Re: Dealing with Carriage Returns/New Lines

Posted: 17 Jun 2015 00:58
by highend
Show me a screenshot of (a standard) vim / gvim with the expected output (^P / ^M converted to linebreaks / or not...).

Re: Dealing with Carriage Returns/New Lines

Posted: 17 Jun 2015 01:02
by SkyFrontier
Sure...
(creamVim)

Re: Dealing with Carriage Returns/New Lines

Posted: 17 Jun 2015 01:17
by highend
My gvim shows a ^M at the end of line 4:
Unbenannt-1.png
Is that the only ^M that should be converted into a linebreak or does the same apply for the (last) ^M on line 5?

You'd need two regexes to match that...

Re: Dealing with Carriage Returns/New Lines

Posted: 17 Jun 2015 01:29
by SkyFrontier
Weird.
Double-checked: no extra ^M at the line 4, here.

Anyway, it's the ^Ps that are breaking my workflow now: anything I do or aggressively removes all line feeding, or keep both ^P (undesired, as it produces extra paragraphs all over the output) and <crlf> (legit paragraphs).

Re: Dealing with Carriage Returns/New Lines

Posted: 17 Jun 2015 01:38
by highend
I guess the vbscript.dll catches normal linebreaks with ^P as well, effectively removing all linebreaks...

You could replace existing linebreaks (to protect them) with something unique first (maybe a unicode char or anything else that doesn't exist in a document) then replace your ^P and in the last step the unique "thing" back to a normal linefeed...

E.g.:

Code: Select all

$a = readfile("D:\Users\Highend\Downloads\NULLcharactersSample_2015.xys");
    $a = regexreplace($a, "\x0D$", "@");
    $a = regexreplace($a, "(\x0A|\x10|\x0D)");
    $a = regexreplace($a, "\x40", "<crlf>");
    writefile("D:\Users\Highend\Downloads\NULLcharactersSample_2015-1.xys", $a);
No, not pretty but it should work...

<- Bed

Re: Dealing with Carriage Returns/New Lines

Posted: 17 Jun 2015 01:58
by SkyFrontier
Yes, I'll try that as a last resource.
Thanks much.