473,385 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

regular expression help..

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
7 1990
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bradley Plett | last post by:
I'm hopeless at regular expressions (I just don't use them often enough to gain/maintain knowledge), but I need one now and am looking for help. I need to parse through a document to find a URL,...
4
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go...
10
by: Lee Kuhn | last post by:
I am trying the create a regular expression that will essentially match characters in the middle of a fixed-length string. The string may be any characters, but will always be the same length. In...
3
by: James D. Marshall | last post by:
The issue at hand, I believe is my comprehension of using regular expression, specially to assist in replacing the expression with other text. using regular expression (\s*) my understanding is...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
9
by: Pete Davis | last post by:
I'm using regular expressions to extract some data and some links from some web pages. I download the page and then I want to get a list of certain links. For building regular expressions, I use...
3
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
3
by: Mr.Steskal | last post by:
Posted: Wed Jul 11, 2007 7:01 am Post subject: Regular Expression Help -------------------------------------------------------------------------------- I need help writing a regular...
18
by: Lit | last post by:
Hi, I am looking for a Regular expression for a password for my RegExp ValidationControl Requirements are, At least 8 characters long. At least one digit At least one upper case character
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.