Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Space and the /x modifier

by Django (Pilgrim)
on Aug 29, 2002 at 07:43 UTC ( [id://193696]=perlquestion: print w/replies, xml ) Need Help??

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

Dear monks,
I recently found out, that \s, like any other of those metachars, can't be used s//here/, since it represents a character class. So how can I put space s//there/x? A literal space is discarded by /x. I could use a variable like $Space or play around with chr 32, but is there a straighter solution?

~Django
"Why don't we ever challenge the spherical earth theory?"

Replies are listed 'Best First'.
Re: Space and the /x modifier
by spurperl (Priest) on Aug 29, 2002 at 08:11 UTC
    From perldoc perlre:
    x Extend your pattern's legibility by permitting whitespace and comments. ... ... The /x modifier itself needs a little more explanation. It tells the regular expression parser to ignore whitespace that is not backslashed or within a character class. You can use this modifier to break up your regular expression into (slightly) more readable parts.
    It *can* be inferred from here that /x applies only to the first part of s///, but it's not really conclusive. Perhaps this can be clarified with a brief note.
Re: Space and the /x modifier
by sauoq (Abbot) on Aug 29, 2002 at 07:52 UTC
    /x doesn't discard space in the replacement half of the substitution. It only ignores it in the pattern.
    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Space and the /x modifier
by Zaxo (Archbishop) on Aug 29, 2002 at 07:54 UTC

    The /x switch only applies to the regex:

    $ perl -e'$_="abracadabra"; s/ a / /xg; print' br c d br $

    After Compline,
    Zaxo

Re: Space and the /x modifier
by PodMaster (Abbot) on Aug 29, 2002 at 11:21 UTC
    others have said you ought to read perlre, and you should do that
    but you can use \x20, whera as hex(20) is 32, and use of \x20 in a regex will match a space (same if you print "\x20")
    perl -e " print 1 if q{ } =~ /\x20/ "

    ____________________________________________________
    ** The Third rule of perl club is a statement of fact: pod is sexy.

      That's neat! Not necessary for an ordinary space as I can see from the answers above, but quite useful for some exotic chars. Thanx a lot!

      ~Django

      "Why don't we ever challenge the spherical earth theory?"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://193696]
Approved by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-23 11:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found