need to get only the numbers from this string
Moderator: General Moderators
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).
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.
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.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
You're right (in a long way around itanjanesh 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.
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
Ok. But I dont understand how would you tell it to search from begining to end of the string without using ^ and $.
returns Array ( [0] => )
Code: Select all
<?php
$string = "bundled (2.0.28 compatible)";
$pattern = "/[\d\.]*/i"; // $pattern = "/([\d\.]*)/i";
preg_match($pattern,$string,$matches);
print_r($matches);
?>- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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.
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
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;;The "i" modifier is not doing anything here neither. "i" means case insensitive and we're looking for numbers
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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.Skara wrote:I think index 1 means it starts at the 8th character.
In other words. Because you only want the entire pattern from this, you dont need the parens (like my example above shows)