phpregexvalidationversion-numbering

Validate the Android version number in string is 3 or higher with regex


I need to validate that the Android version in a big string is more than 3.

In other words, "Android 2" will be invalid, but "Android 3" and "Android 7" will be correct.


Solution

  • preg_match('/some\s*string\s*([3-9][0-9]*|[1-9][0-9]+)/i', $haystack);
    

    And here the working example

    But, after examining your use-case, which seems to be checking for a specific version in an application description, I too would advise you to just get the number out of the string and compare it to an actual number to be sure it's larger or equal than 3:

    preg_match('/([0-9]+)/', $string, $matches);
    if ($matches[1] >= 3) {
      // Do something
    }