Regexp look-behind to match internet speeds

Posted by Sandman on Stack Overflow See other posts from Stack Overflow or by Sandman
Published on 2010-04-20T10:30:04Z Indexed on 2010/04/20 10:33 UTC
Read the original article Hit count: 260

Filed under:
|

So the user may search for "10 mbit" after which I want to capture the "10" so I can use it in a speed-search rather than a string-search. This isn't a problem, the below regexp does this fine:

if (preg_match("/(\d+)\smbit/", $string)){ ... }

But, the user may search for something like "10/10 mbit" or "10-100 mbit". I don't want to match those with the above regexp - they should be handled in another fashion. So I would like a regexp that matches "10 mbit" if the number is all-numeric as a whole word (i.e. contained by whitespace, newline or lineend/linestart)

Using lookbehind, I did this:

if (preg_match("#(?<!/)(\d+)\s+mbit#i", $string)){

Just to catch those that doesn't have "/" before them, but this matched true for this string: "10/10 mbit" so I'm obviously doing something wrong here, but what?

© Stack Overflow or respective owner

Related posts about regex

Related posts about php