Click here to get back home

laziest / fastest way to match last characters of a string

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
laziest / fastest way to match last characters of a string hofer 09-11-2008
Get Chitika Premium
Posted by hofer on September 11, 2008, 12:00 pm
Please log in for more thread options


Hi,
Let's look at following example:

$text = "Today is a nice day";
$end = "day";

print "text ends with $end" if $text =~ /$end$/;

Would the regular expression be efficient for long strings?

The alternative is a little more awkward to type

print "text ends with $end" substr($text,-length($end)) eq $end; # I
didn't try this line, but it should work I think

Is there any core module containing something like
print "text ends with $end" if endswith($text,$end);


thans and bye


H

Posted by J. Gleixner on September 11, 2008, 12:06 pm
Please log in for more thread options


hofer wrote:
> Hi,
> Let's look at following example:
>
> $text = "Today is a nice day";
> $end = "day";
>
> print "text ends with $end" if $text =~ /$end$/;
>
> Would the regular expression be efficient for long strings?

Why not benchmark some different alternatives to see? Your 'long
strings' might not be all that long.

>
> The alternative is a little more awkward to type
>
> print "text ends with $end" substr($text,-length($end)) eq $end; # I
> didn't try this line, but it should work I think
>
> Is there any core module containing something like
> print "text ends with $end" if endswith($text,$end);

Don't know if it'll be faster, but using length and index would be an
alternative, another would be substr.

perldoc -f index
perldoc -f length
perldoc -f substr

Posted by Ben Morrow on September 11, 2008, 12:23 pm
Please log in for more thread options



>
> $text = "Today is a nice day";
> $end = "day";
>
> print "text ends with $end" if $text =~ /$end$/;
>
> Would the regular expression be efficient for long strings?

~% perl -Mre=debug -e'$end="day"; "Today is a nice day" =~ /$end$/'
Freeing REx: `","'
Compiling REx `day$'
size 4 Got 36 bytes for offset annotations.
first at 1
1: EXACT <day>(3)
3: EOL(4)
4: END(0)
anchored "day"$ at 0 (checking anchored isall) minlen 3
Offsets: [4]
1[3] 0[0] 4[1] 5[0]
Guessing start of match, REx "day$" against "Today is a nice day"...
Found anchored substr "day"$ at offset 16...
Starting position does not contradict /^/m...
Guessed: match at offset 16
Freeing REx: `"day$"'

The first thing it tries is a direct match against the last three
characters, which is as fast as it gets.

Ben

--
Outside of a dog, a book is a man's best friend.
Inside of a dog, it's too dark to read.
ben@morrow.me.uk Groucho Marx

Posted by Jürgen Exner on September 11, 2008, 2:51 pm
Please log in for more thread options


>$text = "Today is a nice day";
>$end = "day";
>print "text ends with $end" if $text =~ /$end$/;
>
>Would the regular expression be efficient for long strings?
>
>The alternative is a little more awkward to type
>
>print "text ends with $end" substr($text,-length($end)) eq $end; # I
>didn't try this line, but it should work I think

These two versions do very different things. If you need REs, then the
second version won't do you any good.
If you want textual comparison without RE-behaviour then the first
version is wrong unless you have a very limited set of possible data.

Use the one that matches your needs. Usually correct is more important
than fast.

jue

Posted by hofer on September 11, 2008, 4:45 pm
Please log in for more thread options



> >print "text ends with $end" if $text =3D~ /$end$/;
>
> >print "text ends with $end" =A0substr($text,-length($end)) eq $end; =A0#=
I
>
> These two versions do very different things. If you need REs, then the
> second version won't do you any good.
> If you want textual comparison without RE-behaviour then the first
> version is wrong unless you have a very limited set of possible data.
>
> Use the one that matches your needs. Usually correct is more important
> than fast.
>
Hi Juergen,

In fact I don't need REs and the finishing strings won't contain
backslashes, dots or other characters, that could be taken as RE.

So in my special case both are interchangable.

For me the RE is visualy more intuitive than the substr with the -
length() and the fact, that the string to be searched has
to be entered twice if it were a constant and not a variable

I just wondered if perl has a built-in string_ends_with() function or
whether REs would be much slower.

As it Ben pointed out the first thing the RE search does is checking
at the end of the string, so I guess I'll stick with REs


bye


N


Similar ThreadsPosted
How to match characters in different locations within string May 21, 2006, 1:45 am
Fastest way to find a match? March 12, 2008, 7:34 pm
FAQ 6.22: How can I match strings with multibyte characters? November 17, 2004, 6:03 pm
FAQ 6.22 How can I match strings with multibyte characters? January 26, 2005, 12:03 am
FAQ 6.22 How can I match strings with multibyte characters? April 19, 2005, 11:03 pm
FAQ 6.22 How can I match strings with multibyte characters? July 5, 2005, 4:03 am
FAQ 6.22 How can I match strings with multibyte characters? October 14, 2005, 4:03 am
FAQ 6.22 How can I match strings with multibyte characters? December 24, 2005, 5:03 pm
FAQ 6.22 How can I match strings with multibyte characters? January 17, 2006, 11:03 pm
FAQ 6.22 How can I match strings with multibyte characters? May 4, 2006, 9:03 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap