Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: regexp: least inclusive match?

by Sidhekin (Priest)
on Mar 02, 2007 at 23:46 UTC ( #602988=note: print w/replies, xml ) Need Help??


in reply to regexp: least inclusive match?

If the marker is a plain string (as in the example, without regex specials), substr and rindex ought to do the job:

my $idx = rindex($text, "Tags"); if ($idx > -1) { substr($text, $idx) = ''; }

Otherwise you could for instance take advantage of * being greedy from the start:

$text =~ s/(.*)Tags.*/$1/s;

print "Just another Perl ${\(trickster and hacker)},"
The Sidhekin proves Sidhe did it!

Replies are listed 'Best First'.
Re^2: regexp: least inclusive match?
by varian (Chaplain) on Mar 03, 2007 at 11:03 UTC
    Sidhekin is right, with a minor change the regex will work.

    This is because principle 0 of regex says

    Principle 0: Taken as a whole, any regexp will be matched at the earliest possible position in the string
    So while your '.+?' dictates a preference for the smallest amount of characters to match, in the end this preference is overruled.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://602988]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2018-09-20 00:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Eventually, "covfefe" will come to mean:













    Results (170 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!