Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^3: Reg exp questions

by davido (Cardinal)
on Nov 09, 2014 at 17:32 UTC ( [id://1106627]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Reg exp questions
in thread Reg exp questions

First thing: You don't need [...] if there is only one element in your character class. \h alone is already a metasymbol that specifies a character class, so if all you want is horizontal whitespace, you could say:

s/\h/_/g;

You asked how to permit it to accept more than one adjacent whitespace, and how to replace any number of adjacent whitespaces with a single underscore:

s/\h+/_/g

If by \h you really just mean 0x20 (chr 32), you could use transliteration with "squash" instead (see perlop):

tr/ /_/s;

This will be a little more efficient than substitution, but tr/// is far less flexible, as it doesn't have regex semantics; there is no pattern, only a list of characters to be transliterated.


Dave

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-03-29 02:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found