By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,829 Members | 2,308 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,829 IT Pros & Developers. It's quick & easy.

regular expression help..

P: n/a
I need some help with regulars...

how can I check if $var contains only "----" (minus) no metter how many??

Thanx...

point
Jul 16 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Thu, 28 Aug 2003 15:50:49 +0200
"point" <po***@caanNOSPAMproduction.com> wrote:
I need some help with regulars...

how can I check if $var contains only "----" (minus) no metter how
many??

Thanx...

point


Don't cross post, it's considered bad form.

/^-+$/

Begining of the line (^), followed by minus (-) one or more times (+)
followed by end of line ($)

There are some very good online tutorial for regexs; try here
http://sitescooper.org/tao_regexps.html
Matt

--
Quispiam Power Computing | "There are two major products that come out
Pendle Hill, Australia | of Berkeley: LSD and UNIX. We don't believe
+61 2 9631 7719 | this to be a coincidence. "
www.quispiam.com | - Jeremy S. Anderson
Jul 16 '05 #2

P: n/a
On Thu, 28 Aug 2003 15:50:49 +0200 in
<message-id:bi*********@enews1.newsguy.com>
"point" <po***@caanNOSPAMproduction.com> wrote:
I need some help with regulars...

how can I check if $var contains only "----" (minus) no metter how
many??

Thanx...

point

if (preg_match('/^([-]+)$/', $var)) {
echo "All's good\n";
} else {
echo "Oops.. illegal chars!\n";
}
HTH =)

Regards,

Ian

--
Ian.H [Design & Development]
digiServ Network - Web solutions
www.digiserv.net | irc.digiserv.net | forum.digiserv.net
Programming, Web design, development & hosting.
Jul 16 '05 #3

P: n/a
On Thu, 28 Aug 2003 14:48:25 GMT, "Ian.H [dS]" <ia*@WINDOZEdigiserv.net> wrote:
preg_match('/^([-]+)$/', $var)


Just a tip - don't capture unless you actually want to use the subexpressions,
and there's no point having a character class of a single character.

/^-+$/ is enough here.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 16 '05 #4

P: n/a
On Thu, 28 Aug 2003 16:27:42 +0100 in
<message-id:2n********************************@4ax.com>
Andy Hassall <an**@andyh.co.uk> wrote:
On Thu, 28 Aug 2003 14:48:25 GMT, "Ian.H [dS]"
<ia*@WINDOZEdigiserv.net> wrote:
preg_match('/^([-]+)$/', $var)


Just a tip - don't capture unless you actually want to use the
subexpressions,
and there's no point having a character class of a single character.

/^-+$/ is enough here.

Thanks Andy.. after I replied and re-synced the groups.. I noticed the
reply without the match / group chars.. I guess it's one of those things
where "I used it before.. it works..." and I haven't really got around
to try and improve it.

Good tip and noted.. especially as I use lots of regex for various
projects =)

Regards,

Ian

--
Ian.H [Design & Development]
digiServ Network - Web solutions
www.digiserv.net | irc.digiserv.net | forum.digiserv.net
Programming, Web design, development & hosting.
Jul 16 '05 #5

P: n/a
point wrote:

I need some help with regulars...

how can I check if $var contains only "----" (minus) no metter how many??


Just as a point of interest, I compared the following 3 regular expressions:
/^-+$/, /[^-]/, /^([-]+)$/

I was going to suggest !preg_match("/[^-]/",$val), then realized it wouldn't
return false on an empty string, but I threw it in anyway. The code I used
follows the comparions. In just about ever case /^-+/ won. No surprised there,
just thought I find out how much different they are. I reordered the regex
array 3 times to make sure the array position wasn't biasing the results.

------

/^-+$/ AVG: 4.4089375120221E-05 seconds (363 sampled)
/[^-]/ AVG: 4.4289953219965E-05 seconds (322 sampled)
/^([-]+)$/ AVG: 4.4381050836472E-05 seconds (315 sampled)
-----------------------------------------3--

/^-+$/ AVG: 4.7261700218106E-05 seconds (324 sampled)
/[^-]/ AVG: 5.3900497155554E-05 seconds (353 sampled)
/^([-]+)$/ AVG: 5.3713196202328E-05 seconds (323 sampled)
--23333333333333333333-------

/^-+$/ AVG: 4.3738299402697E-05 seconds (290 sampled)
/[^-]/ AVG: 4.3901622804821E-05 seconds (351 sampled)
/^([-]+)$/ AVG: 4.5026909342051E-05 seconds (359 sampled)
--3------------------------------------------

/^-+$/ AVG: 4.3636065157915E-05 seconds (349 sampled)
/[^-]/ AVG: 4.4002504405861E-05 seconds (334 sampled)
/^([-]+)$/ AVG: 4.4881733434057E-05 seconds (317 sampled)
TESTINGTESTINGTESTING

/^-+$/ AVG: 4.2770132922486E-05 seconds (298 sampled)
/[^-]/ AVG: 4.4013239057577E-05 seconds (367 sampled)
/^([-]+)$/ AVG: 4.2876200889474E-05 seconds (335 sampled)
REORDER ARRAY
------

/^([-]+)$/ AVG: 4.5152124530541E-05 seconds (334 sampled)
/^-+$/ AVG: 4.4505271685899E-05 seconds (338 sampled)
/[^-]/ AVG: 4.5173778766539E-05 seconds (328 sampled)
-----------------------------------------3--

/^([-]+)$/ AVG: 5.4088808734965E-05 seconds (322 sampled)
/^-+$/ AVG: 4.7325738364301E-05 seconds (341 sampled)
/[^-]/ AVG: 5.387234051079E-05 seconds (337 sampled)
--23333333333333333333-------

/^([-]+)$/ AVG: 4.4645499606867E-05 seconds (331 sampled)
/^-+$/ AVG: 4.3906505752659E-05 seconds (318 sampled)
/[^-]/ AVG: 4.4383894004713E-05 seconds (351 sampled)
--3------------------------------------------

/^([-]+)$/ AVG: 4.4498060430799E-05 seconds (336 sampled)
/^-+$/ AVG: 4.3501577726225E-05 seconds (328 sampled)
/[^-]/ AVG: 4.3935364200955E-05 seconds (336 sampled)
TESTINGTESTINGTESTING

/^([-]+)$/ AVG: 4.281405290943E-05 seconds (326 sampled)
/^-+$/ AVG: 4.2799590290457E-05 seconds (345 sampled)
/[^-]/ AVG: 4.3385659307694E-05 seconds (329 sampled)

REORDER ARRAY
------

/[^-]/ AVG: 4.403849682176E-05 seconds (332 sampled)
/^([-]+)$/ AVG: 4.4901682613106E-05 seconds (329 sampled)
/^-+$/ AVG: 4.3864447107006E-05 seconds (339 sampled)
-----------------------------------------3--

/[^-]/ AVG: 5.405314904047E-05 seconds (339 sampled)
/^([-]+)$/ AVG: 5.3905426187718E-05 seconds (329 sampled)
/^-+$/ AVG: 4.7486948679729E-05 seconds (332 sampled)
--23333333333333333333-------

/[^-]/ AVG: 4.3712762685922E-05 seconds (325 sampled)
/^([-]+)$/ AVG: 4.420560948989E-05 seconds (323 sampled)
/^-+$/ AVG: 4.3457881970839E-05 seconds (352 sampled)
--3------------------------------------------

/[^-]/ AVG: 4.452014041889E-05 seconds (329 sampled)
/^([-]+)$/ AVG: 4.4954001013912E-05 seconds (335 sampled)
/^-+$/ AVG: 4.4005612532298E-05 seconds (336 sampled)
TESTINGTESTINGTESTING

/[^-]/ AVG: 4.2576365675663E-05 seconds (326 sampled)
/^([-]+)$/ AVG: 4.18363008818E-05 seconds (329 sampled)
/^-+$/ AVG: 4.1880469391311E-05 seconds (345 sampled)

---------------- START CODE

<plaintext>
<?php
$str = array("------", "-----------------------------------------3--",
"--23333333333333333333-------",
"--3------------------------------------------", "TESTINGTESTINGTESTING");

function getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}

foreach($str as $strsample) {
$res = array(array("/[^-]/",0,0), array("/^([-]+)$/",0,0), array("/^-+$/",0,0));
for($i=0;$i<1000;++$i) {
$rnd = rand(0,2);
$time_start = getmicrotime();
preg_match($res[$rnd][0], $strsample);
$time_end = getmicrotime();
$res[$rnd][1]++;
$res[$rnd][2] += ($time_end - $time_start);
}

echo "\n\n ".$strsample."\n\n";

foreach($res as $val) {
echo " ".$val[0];
for($i=strlen($val[0]);$i<10;++$i) {
echo " ";
}
echo("\tAVG: ".($val[2] / $val[1])." seconds (".$val[1]." sampled)\n");
}
}

?>
---------------- END CODE

Shawn
--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com
Jul 16 '05 #6

P: n/a
Shawn Wilson wrote:
Just as a point of interest, I compared the following 3 regular expressions:

<snip>
Wow. I reread my post and am embarrassed by the number of spelling and grammar
mistakes. In case anyone is wondering, English is my first language.

Shawn
--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com
Jul 16 '05 #7

P: n/a
I forgott to say: Thanx guys :)))
"point" <po***@caanNOSPAMproduction.com> wrote in message
news:bi*********@enews1.newsguy.com...
I need some help with regulars...

how can I check if $var contains only "----" (minus) no metter how many??

Thanx...

point

Jul 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.