how to do this with RE?

Discussion in 'Perl Misc' started by Arthur, Dec 1, 2006.

  1. Arthur

    Arthur Guest

    Hi,

    $foo is a string with leading spaces of variable number, like

    " Willy"

    What I want is to replace the leading spaces with   of the same
    number as in $foo, so that

    " Willy"

    becomes

    "   Willy"

    (Don't know if I make myself understood?) How can I do this?

    Thanks,
    Arthur
    Arthur, Dec 1, 2006
    #1
    1. Advertising

  2. Arthur <> wrote:


    > $foo is a string with leading spaces of variable number, like
    >
    > " Willy"



    That is a pretty long-winded way of saying:

    $foo = " Willy";

    Have you seen the Posting Guidelines that are posted here frequently?


    > What I want is to replace the leading spaces with &nbsp; of the same
    > number as in $foo, so that
    >
    > " Willy"
    >
    > becomes
    >
    > "&nbsp;&nbsp;&nbsp;Willy"



    $foo =~ s/^(\s+)/ '&nbsp;' x length $1/e;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Dec 1, 2006
    #2
    1. Advertising

  3. Arthur

    John Bokma Guest

    Arthur <> wrote:

    > Hi,
    >
    > $foo is a string with leading spaces of variable number, like
    >
    > " Willy"
    >
    > What I want is to replace the leading spaces with &nbsp; of the same
    > number as in $foo, so that
    >
    > " Willy"
    >
    > becomes
    >
    > "&nbsp;&nbsp;&nbsp;Willy"
    >
    > (Don't know if I make myself understood?) How can I do this?


    I guess your problem has nothing to do with Perl but with a lack of
    understanding of HTML / CSS. Normally if you want to move Willy to the
    right you use a margin on the container element.

    --
    John Experienced Perl programmer: http://castleamber.com/

    Perl help, tutorials, and examples: http://johnbokma.com/perl/
    John Bokma, Dec 1, 2006
    #3
  4. Arthur

    Arthur Guest

    Tad McClellan Wrote:
    > Arthur <> wrote:
    >
    >> $foo is a string with leading spaces of variable number, like
    >> " Willy"

    > That is a pretty long-winded way of saying:
    >
    > $foo = " Willy";
    >
    > Have you seen the Posting Guidelines that are posted here frequently?
    >
    >> What I want is to replace the leading spaces with &nbsp; of the same
    >> number as in $foo, so that
    >> " Willy"
    >> becomes
    >>
    >> "&nbsp;&nbsp;&nbsp;Willy"

    >
    > $foo =~ s/^(\s+)/ '&nbsp;' x length $1/e;
    >


    Thanks, and apologies for my wordiness :p

    Arthur
    Arthur, Dec 1, 2006
    #4
  5. Arthur

    Arthur Guest

    John Bokma дµÀ:
    > Arthur <> wrote:
    >
    >> Hi,
    >>
    >> $foo is a string with leading spaces of variable number, like
    >>
    >> " Willy"
    >>
    >> What I want is to replace the leading spaces with &nbsp; of the same
    >> number as in $foo, so that
    >>
    >> " Willy"
    >>
    >> becomes
    >>
    >> "&nbsp;&nbsp;&nbsp;Willy"
    >>
    >> (Don't know if I make myself understood?) How can I do this?

    >
    > I guess your problem has nothing to do with Perl but with a lack of
    > understanding of HTML / CSS. Normally if you want to move Willy to the
    > right you use a margin on the container element.
    >

    In fact I wasn't trying to move Willy to the right. Some of the postings
    on my web forum are written as poems, with some lines like that, so I
    just want to display the lines as intended. (Trailing spaces cause
    problems if replaced with &nbsp; because very long lines don't wrap
    properly.)
    Arthur, Dec 1, 2006
    #5
  6. Arthur

    Arthur Guest

    Andrew DeFaria дµÀ:
    > Arthur wrote:
    >> In fact I wasn't trying to move Willy to the right. Some of the
    >> postings on my web forum are written as poems, with some lines like
    >> that, so I just want to display the lines as intended. (Trailing
    >> spaces cause problems if replaced with &nbsp; because very long lines
    >> don't wrap properly.)

    > <pre>
    >
    > --
    >
    > Andrew DeFaria <http://defaria.com>
    > All women are idiots... and I married their queen.


    I considered <pre>, but PREed paragraphs don't seem to wrap?
    Arthur, Dec 1, 2006
    #6
  7. Arthur wrote:

    > $foo is a string with leading spaces of variable number, like
    >
    > " Willy"
    >
    > What I want is to replace the leading spaces with &nbsp; of the same
    > number as in $foo, so that
    >
    > " Willy"
    >
    > becomes
    >
    > "&nbsp;&nbsp;&nbsp;Willy"
    >
    > (Don't know if I make myself understood?) How can I do this?
    >


    Another way:

    $foo = " Willy";
    $foo =~ s/\G\s/&nbsp;/g;

    print $foo; # -> &nbsp;&nbsp;&nbsp;Willy

    --
    Charles DeRykus
    Charles DeRykus, Dec 1, 2006
    #7
  8. Arthur

    Steve K. Guest

    Tad McClellan wrote:
    > Arthur <> wrote:
    >
    >
    > > $foo is a string with leading spaces of variable number, like
    > >
    > > " Willy"

    >
    >
    > That is a pretty long-winded way of saying:
    >
    > $foo = " Willy";
    >
    > Have you seen the Posting Guidelines that are posted here frequently?


    I don't see them at all in my spool. I'm not sure when the last time it
    was posted (haven't seen them in a while come to think of it), it's also
    possible others my not be seeing it too. But then again, you and others
    have been told this before by others.
    Steve K., Dec 1, 2006
    #8
  9. Arthur

    Dr.Ruud Guest

    Charles DeRykus schreef:
    > Arthur:


    >> What I want is to replace the leading spaces with &nbsp;

    >
    > $foo = " Willy";
    > $foo =~ s/\G\s/&nbsp;/g;


    Nice example of \G usage.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Dec 1, 2006
    #9
  10. Arthur wrote:
    > Andrew DeFaria дµÀ:
    >> Arthur wrote:
    >>> In fact I wasn't trying to move Willy to the right. Some of the
    >>> postings on my web forum are written as poems, with some lines like
    >>> that, so I just want to display the lines as intended. (Trailing
    >>> spaces cause problems if replaced with &nbsp; because very long
    >>> lines don't wrap properly.)

    > I considered <pre>, but PREed paragraphs don't seem to wrap?

    First you say you want it to remain as intended and now you want to
    alter it! Make up your mind! ;-)

    You could try:

    pre {
    white-space: pre-wrap; /* css-3 */
    white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
    white-space: -pre-wrap; /* Opera 4-6 */
    white-space: -o-pre-wrap; /* Opera 7 */
    word-wrap: break-word; /* Internet Explorer 5.5+ */
    }

    --

    Andrew DeFaria <http://defaria.com>
    Even a mosquito doesn't get a slap on the back until it starts to work.
    Andrew DeFaria, Dec 1, 2006
    #10
  11. On 12/01/2006 06:37 AM, Dr.Ruud wrote:
    > Charles DeRykus schreef:
    >> Arthur:

    >
    >>> What I want is to replace the leading spaces with &nbsp;

    >> $foo = " Willy";
    >> $foo =~ s/\G\s/&nbsp;/g;

    >
    > Nice example of \G usage.
    >


    I'm a little confused. How does \G change the statement? Wouldn't the
    result be exactly the same without it?


    --
    Mumia W. (reading news), Dec 1, 2006
    #11
  12. Arthur

    Jim Gibson Guest

    In article <YN2ch.6254$>,
    reading news <> wrote:

    > On 12/01/2006 06:37 AM, Dr.Ruud wrote:
    > > Charles DeRykus schreef:
    > >> Arthur:

    > >
    > >>> What I want is to replace the leading spaces with &nbsp;
    > >> $foo = " Willy";
    > >> $foo =~ s/\G\s/&nbsp;/g;

    > >
    > > Nice example of \G usage.
    > >

    >
    > I'm a little confused. How does \G change the statement? Wouldn't the
    > result be exactly the same without it?


    Why don't you try it. Then try it on the string " Willy Wonka " and
    see what happens.
    Jim Gibson, Dec 2, 2006
    #12
  13. Arthur

    DJ Stunks Guest

    Dr.Ruud wrote:
    > Charles DeRykus schreef:
    > > Arthur:

    >
    > >> What I want is to replace the leading spaces with &nbsp;

    > >
    > > $foo = " Willy";
    > > $foo =~ s/\G\s/&nbsp;/g;

    >
    > Nice example of \G usage.


    agreed, but allow me to weigh in with

    $foo =~ s{^ \s+ }{ '&nbsp;' x $+[0] }xe;

    just out of boredom's sake :p

    -jp
    DJ Stunks, Dec 2, 2006
    #13
  14. Arthur

    DJ Stunks Guest

    Mirco Wahab wrote:
    > Thus spoke Mumia W. (reading news) (on 2006-12-02 00:21):
    >
    > > On 12/01/2006 06:37 AM, Dr.Ruud wrote:
    > >> Charles DeRykus schreef:
    > >>> Arthur:
    > >>
    > >>>> What I want is to replace the leading spaces with &nbsp;
    > >>> $foo = " Willy";
    > >>> $foo =~ s/\G\s/&nbsp;/g;
    > >>
    > >> Nice example of \G usage.
    > >>

    > >
    > > I'm a little confused. How does \G change the statement? Wouldn't the
    > > result be exactly the same without it?

    >
    > I think what was intended is sth. like:
    >
    > $foo =~ s/^\s+/'&nbsp;'x length$&/eg;
    >
    > which is much easier to comprehend
    > with the \G (if I'm not mislead).


    the /g is not required, and use of the $& variable increases overhead
    for every regex in the script though.

    I don't think the @+ or @- arrays have the same performance impact as
    the $& family (they're filled for every regular expression?), but
    correct me if I'm wrong...

    -jp
    DJ Stunks, Dec 2, 2006
    #14
  15. Arthur

    Ben Morrow Guest

    Quoth "DJ Stunks" <>:
    >
    > I don't think the @+ or @- arrays have the same performance impact as
    > the $& family (they're filled for every regular expression?), but
    > correct me if I'm wrong...


    You're not :). The overhead of $& arises from the need to copy the
    string, in case you change the original (or it goes out of scope) before
    you access $&. @+ and @- just give offsets into the original, so they
    don't need to copy anything (and you have to do it yourself, but only if
    and when you need to).

    Ben

    --
    It will be seen that the Erwhonians are a meek and long-suffering people,
    easily led by the nose, and quick to offer up common sense at the shrine of
    logic, when a philosopher convinces them that their institutions are not based
    on the strictest morality. [Samuel Butler, paraphrased]
    Ben Morrow, Dec 2, 2006
    #15
  16. On 12/01/2006 06:02 PM, Jim Gibson wrote:
    > In article <YN2ch.6254$>,
    > reading news <> wrote:
    >
    >> On 12/01/2006 06:37 AM, Dr.Ruud wrote:
    >>> Charles DeRykus schreef:
    >>>> Arthur:
    >>>
    >>>>> What I want is to replace the leading spaces with &nbsp;
    >>>> $foo = " Willy";
    >>>> $foo =~ s/\G\s/&nbsp;/g;
    >>> Nice example of \G usage.
    >>>

    >> I'm a little confused. How does \G change the statement? Wouldn't the
    >> result be exactly the same without it?

    >
    > Why don't you try it. Then try it on the string " Willy Wonka " and
    > see what happens.


    Okay, it only substitutes the spaces at the beginning of the string with
    \G. My eyes glazed over when I started reading the section about \G in
    perlre, so I never understood fully.

    I think I'm beginning to understand why it works the way it does for
    this program; I still don't see how it would be useful elsewhere, but I
    know it's in Perl for a reason. :)

    And now I know why Dr. Ruud said it was a perfect example of using \G.
    The only other ways I can think of for doing the same thing involve
    using the /e option. Good job Mr. DeRykus.


    --
    Mumia W. (reading news), Dec 2, 2006
    #16
  17. Arthur

    Stan R. Guest

    DJ Stunks wrote:
    > Mirco Wahab wrote:
    >> Thus spoke Mumia W. (reading news) (on 2006-12-02 00:21):
    >>
    >>> On 12/01/2006 06:37 AM, Dr.Ruud wrote:
    >>>> Charles DeRykus schreef:
    >>>>> Arthur:
    >>>>
    >>>>>> What I want is to replace the leading spaces with &nbsp;
    >>>>> $foo = " Willy";
    >>>>> $foo =~ s/\G\s/&nbsp;/g;
    >>>>
    >>>> Nice example of \G usage.
    >>>>
    >>>
    >>> I'm a little confused. How does \G change the statement? Wouldn't
    >>> the result be exactly the same without it?

    >>
    >> I think what was intended is sth. like:
    >>
    >> $foo =~ s/^\s+/'&nbsp;'x length$&/eg;
    >>
    >> which is much easier to comprehend
    >> with the \G (if I'm not mislead).

    >
    > the /g is not required, and use of the $& variable increases overhead
    > for every regex in the script though.
    >
    > I don't think the @+ or @- arrays have the same performance impact as
    > the $& family (they're filled for every regular expression?), but
    > correct me if I'm wrong...
    >
    > -jp



    Why use any of them?

    print '<pre>';

    my $s =
    " Willy Wonka \n".
    " has a factory\n".
    " mmmmm\n".
    " chocolate";

    $s =~ s/^(\s+)/'&nbsp;'x length $1/gme;

    print $s, "\n";



    &nbsp;&nbsp;&nbsp;&nbsp;Willy Wonka
    &nbsp;&nbsp;&nbsp;has a factory
    &nbsp;&nbsp;mmmmm
    &nbsp;chocolate
    Stan R., Dec 3, 2006
    #17
  18. Arthur

    Stan R. Guest

    DJ Stunks wrote:
    > Dr.Ruud wrote:
    >> Charles DeRykus schreef:
    >>> Arthur:

    >>
    >>>> What I want is to replace the leading spaces with &nbsp;
    >>>
    >>> $foo = " Willy";
    >>> $foo =~ s/\G\s/&nbsp;/g;

    >>
    >> Nice example of \G usage.

    >
    > agreed, but allow me to weigh in with
    >
    > $foo =~ s{^ \s+ }{ '&nbsp;' x $+[0] }xe;
    >
    > just out of boredom's sake :p
    >
    > -jp


    Yours might work if you added 'm' to the tail end so it works for every
    line.

    --
    Stan
    Stan R., Dec 3, 2006
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page