Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Looking for white spaces between words

by TASdvlper (Monk)
on Jun 10, 2004 at 20:24 UTC ( [id://363182]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all,

Hopefully a quick one for ya. Say I have a string:

my $foo = "the lazy brown fox jumped..."
I want to run a regexp and search for 2 or more consecutive spaces, and if I find it, substitute   for each space. So the above would look like:
my $foo = "the lazy nbsp;brown  &nbspfox  &nb +sp;&nbspjumped..."
Any help would be greatly appreciated.

Replies are listed 'Best First'.
Re: Looking for white spaces between words
by Enlil (Parson) on Jun 10, 2004 at 20:31 UTC
    $foo =~ s/( {2,})/" " x length($1)/ge

    -enlil

      That works great to replace all spaces. However, as a casual observer, I'm not sure this is what was asked. Were all the spaces to be replaced with the non-breaking spaces, or just those after the first one, leaving one space without the replacement. So:
      "the lazy  brown   fox    jumped..."
      But I would like to expand the question a bit. I would like to see all spaces removed with the exception of just one separating the words, and then have that replace with the non-breaking space. So:
      "the lazy brown fox jumped..."

      —Brad
      "A little yeast leavens the whole dough."
        That works great to replace all spaces. However, as a casual observer, I'm not sure this is what was asked. Were all the spaces to be replaced with the non-breaking spaces, or just those after the first one, leaving one space without the replacement. So:
        "the lazy  brown   fox    jumped..."

        This should work:

        $foo =~ s/(?<= )( +)/'&nbsp;' x length($1)/ge

        But I would like to expand the question a bit. I would like to see all spaces removed with the exception of just one separating the words, and then have that replace with the non-breaking space. So:

        "the lazy&nbsp;brown&nbsp;fox&nbsp;jumped..."

        Can be solved thusly:

        $foo =~ s/ {2,}/&nbsp;/g;
        I believe this answers both requests unless I am misreading something.

        -enlil

Re: Looking for white spaces between words
by elusion (Curate) on Jun 10, 2004 at 20:41 UTC
    Well, let's go through this a step at a time. You want to search for 2 or more consecutive spaces. There are two ways to do that: /  +/ and / {2,}/. I normally use the first one, as that's what I think looks best.

    Second, you want to make a substitution. Enter $var =~ s/.../.../;. Now we have this: $foo =~ s/(  +)//;. We just need to come up with something to substitute in there.

    Well, you want &nbsp; for each space, so we want it repeated for the length of the spaces: "&nbsp;" x length $1.

    Now to add that to our regular expression. The trick is to use the /e and /g modifiers, which interpret the second half of the substitution as an expression and substitute globally. $foo =~ s/(  +)/"&nbsp;" x length $1/ge And that's it! You're done!

    Hope this helps,

Re: Looking for white spaces between words
by Roy Johnson (Monsignor) on Jun 10, 2004 at 21:17 UTC
    Just for fun, a lookaround solution. When you find a space preceded by a space, or a space followed by a space, replace it:
    s/(?<= ) | (?= )/&nbsp;/g;

    The PerlMonk tr/// Advocate
Re: Looking for white spaces between words
by fletcher_the_dog (Friar) on Jun 10, 2004 at 20:37 UTC
    As an alternate solution to what you appear to be trying to do you could try:
    my $foo = "the lazy brown fox jumped..."; $foo = "<pre>$foo</pre>";
    The "pre" tag preserves white space in html.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-23 19:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found