need to get only the numbers from this string

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Syranide
Forum Contributor
Posts: 281
Joined: Fri May 20, 2005 3:16 pm
Location: Sweden

Post 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&#39;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).
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :P).

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 :D
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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] => )
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 = &quote;/&#1111;\\d\\.]*/&quote;; // $pattern = &quote;/(&#1111;\d\.]*)/i&quote;;
EDIT | Ooops :P 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 :)
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

$pattern = "/[\\d\\.]*/i";
or
preg_match("/[\d\.]*/i",$str,$matches);

both returning Array ( [0] => )
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

anjanesh wrote:$pattern = "/[\\d\\.]*/i";
or
preg_match("/[\d\.]*/i",$str,$matches);

both returning Array ( [0] => )
See edit above ;)
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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. :?:
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

I think index 1 means it starts at the 8th character.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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) :wink:
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

aaah, ok. I usually don't find a need for preg_match, so I'm not up on it. :P
Post Reply