473,396 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Reg Ex Help for a Lazy VB Programmer

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;
}

}
Jul 19 '05 #1
6 2499
$text = "qwertyuiopasdfghjklzxcvbnm";
$maxLength = 5;
$weblink = 'http://www.google.com';

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

print $text;

result:

qwerthttp://www.google.com


ad******@comcast.net wrote in message news:<dt********************************@4ax.com>. ..
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;
}

}

Jul 19 '05 #2
"*
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;
} *"
ad******@comcast.net 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
-----------------------------------------------------------------------

Jul 19 '05 #3
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, ca**********@yahoo.com (Yaroslav)
wrote:
Jul 19 '05 #4
ad******@comcast.net wrote:
Thank you both for your help. I've been having problems digesting
perlre


Then you may want to start with perlretut.

jue
Jul 19 '05 #5

[TOFU rearranged]


ad******@comcast.net wrote in message
news:<dt********************************@4ax.com>. ..
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 <76**************************@posting.google.com >, Yaroslav
<ca**********@yahoo.com> 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.
Jul 19 '05 #6
ad******@comcast.net 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
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Steven Bethard | last post by:
So I end up writing code like this a fair bit: map = {} for key, value in sequence: map.setdefault(key, ).append(value) This code basically constructs a one-to-many mapping -- each value that...
3
by: Jeff Johnson [MVP: VB] | last post by:
What is the point of lazy * and lazy ? ? "Nothing" will always succeed first, right? If not, can someone give me an example of when either of these might be used?
354
by: Montrose... | last post by:
After working in c# for a year, the only conclusion I can come to is that I wish I knew c. All I need is Linux, the gnu c compiler and I can do anything. Web services are just open sockets...
4
by: Siemel Naran | last post by:
What is a good idiom for handling a lazy object? I see 2 good possibilities. Any more, any comments? Which way do people here use? (1) class Thing { public: Thing(double x, double y) :...
2
by: Bill | last post by:
Hello -- I'm a Java programmer who's slowly getting up to speed in Python. In general I try to initialize the state of my objects as late as possible, in the accessor. So if I have a member...
9
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
39
by: Boltar | last post by:
Why does C do lazy evaluation for logical boolean operations but not bitwise ones? Ie: the following program prints "1 2" , not "1 1" under gcc main() { int a = 1; int b = 1; 0 && ++a;
2
by: fredd00 | last post by:
Hi, i'm trying to use lazy loading with Linq to sql and related objects seems like you can only call the child object if the context is still open, this is not real lazy loading. here is my...
2
by: Michael Bray | last post by:
With the recent release of EF I've decided to dig into it a bit more than I did before... the question I'm specifically interested in, but haven't been able to find a resource to answer it is......
6
by: Peng Yu | last post by:
Hi, I'm wondering if the following assignment is lazy copy or not? Thanks, Peng std::vector<intv. v.push_back(1); v.push_back(2);
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.