Ultra-simple (Handy) Script to Regex Escape Paths (or Whatever) - Leaves Result in Clipboard for Other Use

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
Dustydog
Posts: 321
Joined: 13 Jun 2016 04:19

Ultra-simple (Handy) Script to Regex Escape Paths (or Whatever) - Leaves Result in Clipboard for Other Use

Post by Dustydog »

This is so simple it's almost silly, but I use it, and thought others might.

The reason for it is other applications (and this one) support regular expressions in pathnames (or whatever), for search and replace patterns. Those pathnames often have control characters that are used in regular expressions that need to be escaped before they're usable - like parentheses, for one - and it's a hassle. Though this script defaults to your current item, I generally use it by pasting the clipboard into the dialogue box. It then puts an escaped version of whatever you put into the dialogue box back into the clipboard (and shows it as a message).

If you'd rather have it default to the clipboard, replace <curitem> with <clp> in the script, and I suppose change the dialogue note.

This does not escape the "|" (bar) character as it's illegal in a pathname anyway, and one might be setting up a search with an alternation in it. If you wish, just add it between the brackets.

Again, simple as can be, but I find it useful.

Code: Select all

"Regex Escape"
 $ToEscape = input("Escape for Regex Use", "Current Path + Item is Default; Pasting Clipboard Works Well", <curitem>);
 Copytext regexreplace($ToEscape, "([][{}()*+?.\\^$])", "\$1");
 msg "This is escaped for use in a regex and is in the clipboard:  " . <clp>;
 
The other use for it is if you're editing something that uses regular expressions, your editor allows a regex search and replace, and you need to change something in multiple places in a way that using a regular expression to do it with is useful - in which case there's a whole lot of escaping goin' on and this can be very handy indeed. (That's actually why I wrote it.)

Post Reply