Reg Ex Help for a Lazy VB Programmer

A

adams114

Ok,

I'm trying to count the number of characters in a string. Once the
count of character's reaches x I want to replace the rest of the
string with an web site link. So here's what I've come up with so
far, but I cannot figure out how to do the replace. Can you help me? I
cannot even get what I have to compile.



sub replaceLink {
my ($text) = @_;
my $textLength;
my $url;
my $maxLength;
my $count;
my $newText;

$maxLength = 305;
$textLength = length $_;
$url = "<a href='somwhere.html'>[View Article]</a>";

if ($textLength > 305) {
$count = 0;
while ($text =~ .\g){ # this is the error, . matching
# any character, g for greed
$count++;
if ($count == $maxLength){
$newText = $newText . $url;
}
else {
$newText = $newText . $2;
}

}
return $newText;
}
else {
return $text;
}

}
 
A

adams114

Thank you both for your help. I've been having problems digesting
perlre so your help is deeply appreciated.

I have a couple of questions if you don't mind helping me

() are used to remember a match right? So why the . after ()?
(.{$maxLength}).


Now, if i wanted to match the first whole word to the left of
$maxLength is this a good way to approach it (ensuring that a word is
not cut off) match a word boundry at the anchor?

$text =~ s/(.{$maxLength}).*?$.\b/$1/$weblink/;

On 20 Apr 2004 07:24:04 -0700, (e-mail address removed) (Yaroslav)
wrote:
 
N

nomercy

"*
while ($text =~ .\g){ # this is the error, . matching
# any character, g for greed
*"

s/.// if you want to substitute any one charecter by nothing.
* and ? are the greedy operators, and *? and ?? for non-greedy.
you should realy check perl's regular expressions.
Yaroslav has solved your problem in a better way than this on you
code:
"*
while ($text =~ .\g){ # this is the error, . matching
# any character, g for greed
$count++;
if ($count == $maxLength){
$newText = $newText . $url;
}
*"


*Ok,

I'm trying to count the number of characters in a string. Once the
count of character's reaches x I want to replace the rest of the
string with an web site link. So here's what I've come up with so
far, but I cannot figure out how to do the replace. Can you help me
I
cannot even get what I have to compile.



sub replaceLink {
my ($text) = @_;
my $textLength;
my $url;
my $maxLength;
my $count;
my $newText;

$maxLength = 305;
$textLength = length $_;
$url = "<a href='somwhere.html'>[View Article]</a>";

if ($textLength > 305) {
$count = 0;
while ($text =~ .\g){ # this is the error, . matching
# any character, g for greed
$count++;
if ($count == $maxLength){
$newText = $newText . $url;
}
else {
$newText = $newText . $2;
}

}
return $newText;
}
else {
return $text;
}

}
-
nomerc
 
J

Joe Smith

So why the . after ()?

You're looking at it wrong. There's not just a dot there, you need
to think of .*? as a single unit.

1) Read 'perldoc perlreftut' at least 3 times.
2) Post to comp.lang.perl.misc instead of comp.lang.perl next time.
-Joe
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top