Dealing with Carriage Returns/New Lines

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Dealing with Carriage Returns/New Lines

Post 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.
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

highend
Posts: 14953
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Dealing with Carriage Returns/New Lines

Post by highend »

Did you try "\r?\n"?
One of my scripts helped you out? Please donate via Paypal

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Dealing with Carriage Returns/New Lines

Post by SkyFrontier »

Yes.
Regex engine seems to simply ignore those ones.
Thanks for the feedback.
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

highend
Posts: 14953
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Dealing with Carriage Returns/New Lines

Post by highend »

It's definitely not the most advanced one (vbscript.dll)...

Can you provide a test file?
One of my scripts helped you out? Please donate via Paypal

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Dealing with Carriage Returns/New Lines

Post by SkyFrontier »

Sure.
Attached.
To see the attached files, you need to log into the forum.
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

highend
Posts: 14953
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Dealing with Carriage Returns/New Lines

Post 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);
One of my scripts helped you out? Please donate via Paypal

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Dealing with Carriage Returns/New Lines

Post 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.
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

highend
Posts: 14953
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Dealing with Carriage Returns/New Lines

Post 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));
One of my scripts helped you out? Please donate via Paypal

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Dealing with Carriage Returns/New Lines

Post 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>?
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

highend
Posts: 14953
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Dealing with Carriage Returns/New Lines

Post by highend »

Show me a screenshot of (a standard) vim / gvim with the expected output (^P / ^M converted to linebreaks / or not...).
One of my scripts helped you out? Please donate via Paypal

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Dealing with Carriage Returns/New Lines

Post by SkyFrontier »

Sure...
(creamVim)
To see the attached files, you need to log into the forum.
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

highend
Posts: 14953
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Dealing with Carriage Returns/New Lines

Post 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...
To see the attached files, you need to log into the forum.
One of my scripts helped you out? Please donate via Paypal

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Dealing with Carriage Returns/New Lines

Post 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).
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

highend
Posts: 14953
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Dealing with Carriage Returns/New Lines

Post 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
One of my scripts helped you out? Please donate via Paypal

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Dealing with Carriage Returns/New Lines

Post by SkyFrontier »

Yes, I'll try that as a last resource.
Thanks much.
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

Post Reply