Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex match string that ends with number

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

like image 805
user2217084 Avatar asked May 20 '15 08:05

user2217084


People also ask

How do you check a string is ends with number?

To check if a string ends with a number, call the test() method on a regular expression that matches one or more numbers at the end a string. The test method returns true if the regular expression is matched in the string and false otherwise.

How do you specify the end of a string in regex?

End of String or Line: $ The $ anchor specifies that the preceding pattern must occur at the end of the input string, or before \n at the end of the input string. If you use $ with the RegexOptions. Multiline option, the match can also occur at the end of a line.

Can you use regex with numbers?

Since regular expressions work with text, a regular expression engine treats 0 as a single character, and 255 as three characters. To match all characters from 0 to 255, we'll need a regex that matches between one and three characters. The regex [0-9] matches single-digit numbers 0 to 9.

What does \b mean in regex?

The word boundary \b matches positions where one side is a word character (usually a letter, digit or underscore—but see below for variations across engines) and the other side is not a word character (for instance, it may be the beginning of the string or a space character).


1 Answers

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+$
like image 89
Denys Séguret Avatar answered Oct 05 '22 09:10

Denys Séguret