Page 2 of 2
Posted: Fri Jun 10, 2005 4:38 am
by Syranide
Btw, in reply to the earlier post on learning, Regex Buddy is excellent, I really thought it was <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> til I tried it, has some good sides actually, you'll quickly learn the syntax of PERL with a little fiddeling (be warned though, the regex to PHP-code etc is pretty bogus).
Posted: Fri Jun 10, 2005 1:27 pm
by anjanesh
d11wtq : your patterns are excellent but what If I want 2.0.28 alone ? And not knowing how many dots are in the middle and not searching for ( or )
Array ( [0] => bundled (2.0.28 compatible) [1] => 2.0.28)
anything2.0.28here
anything2.0.5.28here
I thought this $pattern = "/^.*?((\d|\.)*?).*?$/i"; would do thinking it'll search for either a digit or dot until none is found.
Posted: Fri Jun 10, 2005 2:47 pm
by Chris Corbyn
anjanesh wrote:d11wtq : your patterns are excellent but what If I want 2.0.28 alone ? And not knowing how many dots are in the middle and not searching for ( or )
Array ( [0] => bundled (2.0.28 compatible) [1] => 2.0.28)
anything2.0.28here
anything2.0.5.28here
I thought this $pattern = "/^.*?((\d|\.)*?).*?$/i"; would do thinking it'll search for either a digit or dot until none is found.
You're right (in a long way around it

).
Let's simplify it a bit though, because you have unneeded things in your pattern.
Using caret ^ to mark the start of the string followed by .*? is pretty redundant so they can go. Same for the $ and .*? on the RHS.
Now (\d|\.)*?, ok good thinking. [\d\.]* equates to the same thing though, and we can drop the ? since greediness is good here.
$pattern = "/[\d\.]*/";
I had originally included the \( and \) because I knew the number was in between them but the above is much less strict yes

Posted: Fri Jun 10, 2005 2:59 pm
by anjanesh
Ok. But I dont understand how would you tell it to search from begining to end of the string without using ^ and $.
Code: Select all
<?php
$string = "bundled (2.0.28 compatible)";
$pattern = "/[\d\.]*/i"; // $pattern = "/([\d\.]*)/i";
preg_match($pattern,$string,$matches);
print_r($matches);
?>
returns Array ( [0] => )
Posted: Fri Jun 10, 2005 4:05 pm
by Chris Corbyn
It searches from beginning to end anyway. That's how regex work. They read character-for-character the entire string and see what they find (well it's bit more technical than that).
Note: You'll need to escape your backslashes when defining the RegExp outside of the preg_match() like that.
Code: Select all
$pattern = "e;/ї\\d\\.]*/"e;; // $pattern = "e;/(ї\d\.]*)/i"e;;
EDIT | Ooops
That * should be a + ! $pattern = "/[\\d\\.]+/";
The "i" modifier is not doing anything here neither. "i" means case insensitive and we're looking for numbers

Posted: Fri Jun 10, 2005 4:14 pm
by anjanesh
$pattern = "/[\\d\\.]*/i";
or
preg_match("/[\d\.]*/i",$str,$matches);
both returning Array ( [0] => )
Posted: Fri Jun 10, 2005 4:56 pm
by Chris Corbyn
anjanesh wrote:$pattern = "/[\\d\\.]*/i";
or
preg_match("/[\d\.]*/i",$str,$matches);
both returning Array ( [0] => )
See edit above

Posted: Sat Jun 11, 2005 12:47 am
by anjanesh
Thanks d11wtq - Now I fixed my original pattern thanks to you.
$pattern = "/(\\d|\\.)+/";
Array ( [0] => 2.0.28 [1] => 8 )
Ignoring Index 1.

Posted: Sat Jun 11, 2005 11:39 am
by Skara
I think index 1 means it starts at the 8th character.
Posted: Sat Jun 11, 2005 1:17 pm
by Chris Corbyn
Skara wrote:I think index 1 means it starts at the 8th character.
That's not true, 0 is the index of the whole match. 1 is the first parens (which is what anjanesh put as either a digit or a dot). What's happened is that it found 2.0.28 as an entire match and 8 (the last digit) matches the parens. If there were an outer set of parens enclosing that expression and the "+" then it would have 3 outputs.
In other words. Because you only want the entire pattern from this, you dont need the parens (like my example above shows)

Posted: Sun Jun 12, 2005 9:34 am
by Skara
aaah, ok. I usually don't find a need for preg_match, so I'm not up on it.
