how to do this with RE?

A

Arthur

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
 
T

Tad McClellan

$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   of the same
number as in $foo, so that

" Willy"

becomes

"   Willy"


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

John Bokma

Arthur said:
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?

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.
 
A

Arthur

Tad said:
That is a pretty long-winded way of saying:

$foo = " Willy";

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


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

Thanks, and apologies for my wordiness :p

Arthur
 
A

Arthur

John Bokma дµÀ:
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   because very long lines don't wrap
properly.)
 
C

Charles DeRykus

Arthur said:
$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?

Another way:

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

print $foo; # ->    Willy
 
S

Steve K.

Tad said:
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.
 
A

Andrew DeFaria

Arthur said:
Andrew DeFaria дµÀ:
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+ */
}
 
M

Mumia W. (reading news)

Charles DeRykus schreef:

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?
 
J

Jim Gibson

reading news said:
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.
 
D

DJ Stunks

Dr.Ruud said:
Charles DeRykus schreef:

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
 
D

DJ Stunks

Mirco said:
Thus spoke Mumia W. (reading news) (on 2006-12-02 00:21):


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
 
B

Ben Morrow

Quoth "DJ Stunks said:
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
 
M

Mumia W. (reading news)

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.
 
S

Stan R.

DJ said:
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
 
S

Stan R.

DJ said:
Dr.Ruud said:
Charles DeRykus schreef:


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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top