Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

how to express $temp =~ s/.*?from://; in perl 4

by Anonymous Monk
on Jul 30, 2000 at 18:13 UTC ( #25156=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I'm using perl 5.6.0 to program $temp =~ s/.*?from://; this way, however, if i would like to run my program under perl 4 version, i find the ? operation is not allowed. How can i modify my code to run correctly!? THX alot
  • Comment on how to express $temp =~ s/.*?from://; in perl 4

Replies are listed 'Best First'.
Re: how to express $temp =~ s/.*?from://; in perl 4
by splinky (Hermit) on Jul 30, 2000 at 18:45 UTC
    Since you're looking for a constant string, you can use substr and index in place of the regex, like so:

    $temp = substr ( $temp, index ( $temp, 'from:' ) + 5 ) if $temp =~ /from:/;

    Note that, since I don't have Perl 4 installed anywhere, the code above is untested under Perl 4.

    *Woof*

Re: how to express $temp =~ s/.*?from://; in perl 4
by davorg (Chancellor) on Jul 30, 2000 at 19:23 UTC
Re: how to express $temp =~ s/.*?from://; in perl 4
by young perlhopper (Scribe) on Jul 30, 2000 at 21:12 UTC
    I assume from your regex that you are expecting more than one 'from:' to appear in the string and you only want to deal with everything up to the first one. It is easy to simply make the .* nongreedy, but would it be possible for you to make a more specific regular expression that would take into account what comes before or after the from:? i.e. if you are expecting a number to come after this four, but you won't see a number after any other four, then do:
    $temp =~ s/.*from:( \d+)/$1/;
    Unless you are optimizing for speed, I consider it a good habit to make regex's as specific as possible so that you avoid matching unwanted strings without knowing it.

    Hope this helps,
    Mark

Re: how to express $temp =~ s/.*?from://; in perl 4
by Anonymous Monk on Jul 30, 2000 at 22:26 UTC
    Use a negated character class:
    $temp = "notfrom:nowhere"; # Match zero or more non-f characters followed by "from:" $temp =~ s/[^f]*from://; print "$temp\n";
    Prints "nowhere".
      That's not the same as finding the nearest match:
      $text = "fanny farmer from: big sky";
      There's no trivial way to say "a string of text that doesn't contain FROM" in Perl 4 regex. There are many wrong ways that people attempt. :)

      -- Randal L. Schwartz, Perl hacker

RE: how to express $temp =~ s/.*?from://; in perl 4
by Anonymous Monk on Jul 31, 2000 at 08:27 UTC
    What site are you gonna be running this on?
    I want to take advantage of your Perl 4 stupidity and destroy everything on the site.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://25156]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (1)
As of 2022-09-30 19:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (126 votes). Check out past polls.

    Notices?