Regex match string that ends with number

35,672

Solution 1

Just use

\d$

to check your string ends with a digit

If you want your string to be a "c" followed by some digits, use

c\d+$

Solution 2

You can use this regular expression pattern

^c[0-9]+$

Solution 3

To match any string ending with a digit use: [\s\S]*\d$

if (preg_match('/[\s\S]*\d$/', $value)) {
   #match
} else {
  #no match
}
Share:
35,672
Admin
Author by

Admin

Updated on October 01, 2020

Comments

  • Admin
    Admin over 3 years

    What is a regex to match a string that ends with a number for example

    "c1234" - match
    "c12" - match
    "c" - no match
    

    Tried this but it doesn't work

    (?|c(?|[0-9]*$))
    

    Thanks again,

    The beggining string needs to be specific too