Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Reg Ex Help for a Lazy VB Programmer

Reply
Thread Tools

Reg Ex Help for a Lazy VB Programmer

 
 
adams114@comcast.net
Guest
Posts: n/a
 
      04-19-2004
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;
}

}
 
Reply With Quote
 
 
 
 
Yaroslav
Guest
Posts: n/a
 
      04-20-2004
$text = "qwertyuiopasdfghjklzxcvbnm";
$maxLength = 5;
$weblink = 'http://www.google.com';

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

print $text;

result:

qwerthttp://www.google.com




http://www.velocityreviews.com/forums/(E-Mail Removed) wrote in message news:<(E-Mail Removed)>. ..
> 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;
> }
>
> }

 
Reply With Quote
 
 
 
 
adams114@comcast.net
Guest
Posts: n/a
 
      04-20-2004
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 Removed) (Yaroslav)
wrote:


 
Reply With Quote
 
nomercy
Guest
Posts: n/a
 
      04-20-2004
"*
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;
> }

*"


(E-Mail Removed) wrote:
> *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
-----------------------------------------------------------------------
Posted via http://www.codecomments.co
-----------------------------------------------------------------------

 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      04-21-2004
(E-Mail Removed) wrote:
> Thank you both for your help. I've been having problems digesting
> perlre


Then you may want to start with perlretut.

jue


 
Reply With Quote
 
Jim Gibson
Guest
Posts: n/a
 
      04-21-2004

[TOFU rearranged]

>
>
> (E-Mail Removed) wrote in message
> news:<(E-Mail Removed)>. ..
> > 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.
> >
> >


In article <(E-Mail Removed) >, Yaroslav
<(E-Mail Removed)> wrote:

> $text = "qwertyuiopasdfghjklzxcvbnm";
> $maxLength = 5;
> $weblink = 'http://www.google.com';
>
> $text =~ s/(.{$maxLength}).*?$/$1$weblink/;
>


Why not just:

$text = substr($text,0.$maxLength) . $weblink;

?

> print $text;
>
> result:
>
> qwerthttp://www.google.com
>
>


This newsgroup is defunct. Try comp.lang.perl.misc in the future.
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      04-21-2004
(E-Mail Removed) wrote:

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

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
lazy evaluation is sometimes too lazy... help please. Ken Pu Python 3 01-16-2009 11:23 AM
Re: lazy evaluation is sometimes too lazy... help please. Boris Borcic Python 0 01-16-2009 10:46 AM
Re: lazy evaluation is sometimes too lazy... help please. Boris Borcic Python 0 01-16-2009 10:37 AM
hello! first post to clr. I'm asking about an attempt at a lazy rubysolution to computing fibonacci numbers for a project euler problem. seems tobe a bug in lazy ruby... tphyahoo Ruby 6 08-08-2008 08:15 PM
Why it's good to be a "lazy" programmer David Filmer Perl Misc 1 10-14-2004 07:05 AM



Advertisments