Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex?

Status
Not open for further replies.

safra

Technical User
Jan 24, 2001
319
NL
Hi,

The options in a combobox all have something added to it which takes 5 characters.

What would be the quickest way to cut of these 4 variables in the Perl script?

example:

$var = "something (ma)";

should become:

$var = "something";

If I try long enough I might be able to get there with substring but I guess this can be done a lot quicker?

regards,

Ron
 
well, you could you reverse($var);

then take the substring then reverse it again.

or you could use

$var =~ s/^(.+?).{4}$/$1/g;

or something along those lines. i didn't test it, but, it should work. adam@aauser.com
 
A slightly different twist would be to do a substring this way:

my $a = "abcdefghit(kl)";
my $b = substr($a,0,length($a)-4);
print &quot;\$a = <$a>\n&quot;;
print &quot;\$b = <$b>\n&quot;;

Here's the output:
$a = <abcdefghit(kl)>
$b = <abcdefghit>

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
hmm... what the hell was i thinking? reversing does no good in this situation. adam@aauser.com
 
Thanks quys,

Luciddream I tried:

$var =~ s/^(.+?).{4}$/$1/g;

But it didn't seem to work?

I will try your suggestion hmerrill.

btw. yesterday I read something in the perl docs that regex are relatively slowing down the process more than something like hmerrill suggested. Is this true, I thought regex were quite handy and &quot;fast&quot; pieces of code?

Ron,
 
I'd be interested in reading that if you can post what you find that says regex's are slow. I think that some complex regex's may be slower than simple regex's, but I have trouble believing that in general regex's are slow. In fact, I just tested it - here's my test script - and luciddream's $var =~ s/^(.+?).{4}$/$1/g example did work for me - I just found this &quot;times&quot; function today by accident in the Programming Perl O'Reilly book on p.234:

#!/usr/bin/perl -w

use strict;

my ($user, $system, $cuser, $csystem) = times;
my $a = &quot;abcdefghit(kl)&quot;;
my $b = $a;

my $start = (times)[0];

for (my $i = 0; $i < 100000; $i++) {
$b = substr($a,0,length($a)-4);
}
#for (my $i = 0; $i < 100000; $i++) {
# $b =~ s/^(.+).{4}$/$1/g;
#}

my $end = (times)[0];

printf &quot;that took %.2f CPU seconds\n&quot;, $end - $start;

---------------------------------------

Run it one way, then switch which for-loop you comment out, and run it again. Turns out that luciddream's regular expression takes 0.14 cpu seconds, and my substring takes 0.27 cpu seconds - the substring takes about twice as long as the regular expression. That is for 100,000 iterations of a for loop. Pretty interesting - regular expressions are pretty fast :)

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
i've never heard of perl regex's being slow... but, i do know that javascript regex's (modeled after perl's) are practically the slowest processing you can do. adam@aauser.com
 
I read something in the perl docs on I probably misinterpreted it. I'm only a beginner and, as I said, to me regex appeared to be powerful, that was why I was so amazed to read this.

Ron
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top