SavannahLion:

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

In reply to Re: Disable Regex by roboticus
in thread Disable Regex by SavannahLion

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.