regular expression help..

  • Follow


I need some help with regulars...

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

Thanx...

point


0
Reply point5404 (24) 8/28/2003 1:50:49 PM

On Thu, 28 Aug 2003 15:50:49 +0200
"point" <point@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
0
Reply keep1 (18) 8/28/2003 1:36:55 PM


On Thu, 28 Aug 2003 15:50:49 +0200 in
<message-id:bil19u01kqn@enews1.newsguy.com>
"point" <point@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.
0
Reply ian770 (486) 8/28/2003 2:48:25 PM

On Thu, 28 Aug 2003 14:48:25 GMT, "Ian.H [dS]" <ian@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 (andy@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
0
Reply andy171 (2271) 8/28/2003 3:27:42 PM

On Thu, 28 Aug 2003 16:27:42 +0100 in
<message-id:2n7skvkk5ropm0gp5l25vsiqblrn8qebs3@4ax.com>
Andy Hassall <andy@andyh.co.uk> wrote:

> On Thu, 28 Aug 2003 14:48:25 GMT, "Ian.H [dS]"
> <ian@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.
0
Reply ian770 (486) 8/28/2003 4:32:25 PM

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
shawn@glassgiant.com
http://www.glassgiant.com
0
Reply shawn2738 (281) 8/28/2003 7:11:10 PM

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
shawn@glassgiant.com
http://www.glassgiant.com
0
Reply shawn2738 (281) 8/28/2003 7:15:15 PM

I forgott to say: Thanx guys :)))
"point" <point@caanNOSPAMproduction.com> wrote in message
news:bil19u01kqn@enews1.newsguy.com...
> I need some help with regulars...
>
> how can I check if $var contains only "----" (minus) no metter how many??
>
> Thanx...
>
> point
>
>


0
Reply point5404 (24) 9/16/2003 10:54:30 AM

7 Replies
38 Views

(page loaded in 0.104 seconds)

Similiar Articles:













7/15/2012 12:17:46 PM


Reply: