Reg Ex Help for a Lazy VB Programmer

Discussion in 'Perl' started by adams114@comcast.net, Apr 19, 2004.

  1. Guest

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

    }
     
    , Apr 19, 2004
    #1
    1. Advertising

  2. Yaroslav Guest

    $text = "qwertyuiopasdfghjklzxcvbnm";
    $maxLength = 5;
    $weblink = 'http://www.google.com';

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

    print $text;

    result:

    qwerthttp://www.google.com




    wrote in message news:<>...
    > 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;
    > }
    >
    > }
     
    Yaroslav, Apr 20, 2004
    #2
    1. Advertising

  3. Guest

    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, (Yaroslav)
    wrote:
     
    , Apr 20, 2004
    #3
  4. nomercy Guest

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

    *"


    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
    -----------------------------------------------------------------------
     
    nomercy, Apr 20, 2004
    #4
  5. wrote:
    > Thank you both for your help. I've been having problems digesting
    > perlre


    Then you may want to start with perlretut.

    jue
     
    Jürgen Exner, Apr 21, 2004
    #5
  6. Jim Gibson Guest

    [TOFU rearranged]

    >
    >
    > wrote in message
    > news:<>...
    > > 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 <>, Yaroslav
    <> 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.
     
    Jim Gibson, Apr 21, 2004
    #6
  7. Joe Smith Guest

    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
     
    Joe Smith, Apr 21, 2004
    #7
    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.
Similar Threads
  1. Sanny
    Replies:
    391
    Views:
    42,155
    anish.mathew84
    Jan 6, 2010
  2. Ken Pu
    Replies:
    3
    Views:
    681
    Steven D'Aprano
    Jan 16, 2009
  3. Boris Borcic
    Replies:
    0
    Views:
    560
    Boris Borcic
    Jan 16, 2009
  4. Boris Borcic
    Replies:
    0
    Views:
    555
    Boris Borcic
    Jan 16, 2009
  5. David Filmer

    Why it's good to be a "lazy" programmer

    David Filmer, Oct 14, 2004, in forum: Perl Misc
    Replies:
    1
    Views:
    104
    Iain Chalmers
    Oct 14, 2004
Loading...

Share This Page