Regular expressions for url parser

<?php

    $string = 'user34567';

            if(preg_match('/user(^[0-9]{1,8}+$)/', $string)){
                echo 1;
            }

?>

I want to check if a string has the word user number, which can be 8 characters max.

+5
source share
5 answers

You are actually very close:

if(preg_match('/^user[0-9]{1,8}$/', $string)){

The anchor for "must match at the beginning of the line" must be completely in front, and then the "user" literal; then you specify the character set [0-9]and multiplier {1,8}. Finally, you end with the anchor "must match at end of string".

A few comments on your original expression:

  • ^ matches the beginning of the line, so writing it down somewhere else inside this expression, but the beginning will not produce the expected results
  • + - ; {1,8} ,
  • , , .

Btw [0-9] \d. , , ; -)

+10

^ $, , . , ? , :

preg_match( '/^user[0-9]{1,8}[^0-9]$/' , $string );

, :

preg_match( '/user[0-9]{1,8}[^0-9]/' , $string );

, , RegexPal, .

+3

You were close, here is your regular expression: /^user[0-9]{1,8}$/

+2
source

try the following expression:

/^user([0-9]{1,8})$/

+1
source

Use this regex:

/^user\d{1,8}$/
+1
source

All Articles