rjahrman has asked for the wisdom of the Perl Monks concerning the following question:

I'm using the x modifier on one of my regexes to allow me to space everything out and comment it. However, is there a way to use a space that's part of the regex when using the x modifier (and have it not treated as garbage). So if I wanted to replace something with a space, and the x modifier will ignore that space, what can I change it to so that the program will "see" the space?
  • Comment on Using Spaces in a Regex With the X Modifier

Replies are listed 'Best First'.
•Re: Using Spaces in a Regex With the X Modifier
by merlyn (Sage) on Feb 29, 2004 at 19:14 UTC
    Spaces that are escaped by a backslash, defined as \x20 or similar, included in a character class, or referenced by meta-char-classes like \s are still spaces even in /x mode. Most spaces should be matched with \s anyway, because you can't visually distinguish a space from a tab character.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: Using Spaces in a Regex With the X Modifier
by matija (Priest) on Feb 29, 2004 at 19:10 UTC
    The x modifier only ignores spaces in the "match" part, not in the replace part - at least I conclude that from this test:
    perl -e '$_=shift @ARGV; s/ c / /x; print;' "bbbcbbb" bbb bbb
Re: Using Spaces in a Regex With the X Modifier
by gmpassos (Priest) on Mar 01, 2004 at 06:32 UTC
    /[ ]/x
    there is a space.

    Graciliano M. P.
    "Creativity is the expression of the liberty".

Re: Using Spaces in a Regex With the X Modifier
by rjahrman (Scribe) on Feb 29, 2004 at 19:37 UTC
    Thanks!