in reply to Disable Regex
Two things. First, you can save yourself a lot of trouble with Windows paths if you use the '/' instead of '\' characters in your paths. It will work just fine in Windows, and you don't have to worry about the annoying backslashes. (Note: there's a case where it doesn't work reliably, described later.)
Secondly, you'll want to read perlre to learn more about regular expressions and the various tips and tricks.
In your question, you're having two (or more) issues: First, in a regular expression, some character have special meaning. To use them as characters without their special meaning, you need to escape them by preceding them with a '\' character. The * and ? characters are the ones you're having trouble with in this case. So s/foo\*\?/bar?*/ allows you to convert the "foo*?" part of a string to "bar?*". Note: The specialness of the ? and * characters only applies to the regular expression (first) part, not the replacement part, which is why I didn't escape them in the right hand chunk.
The second issue is that in double-quoted strings and regular expressions, the '\' character has a special meaning: Specifically, it's saying "do something different with the following character(s)". In a regular expression, it's saying "treat this as a normal character instead of a regular expression feature". Since the '\' has special meaning, to get one in your double-quoted string or regular expression, you need to escape it by prefixing it with a '\' character. (If you want two '\' characters in a row, you'll have to escape each one, so you'll wind up with four in a row...).
I said "or more", right? What I mean here is that even when you have your string set up with a valid Windows filename, you may still run into problems, depending on how you use it. If you pass the filename into a command shell after that, then you may have another problem: The command shell has its own rules and special characters that you have to fight. (So I avoid calling command shells in my perl scripts.) The rules are different for differing command shells (bash, zsh, etc. in the *NIX world, CMD in the Windows world). So once you have your filename encoded, you still need to understand what the shell is going to do to your filename, so you can further mangle it if necessary. (This is what I meant earlier when I said that there's a case where using '/' instead of '\' won't work reliably: In the CMD shell, it wants to use '/' as an indicator for a command-line switch, so if you pass your filenames to a CMD shell, you'll have to wrestle with the '\' rules...).
...roboticus
|
|---|