Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need a simple RegEx to find a number in a single word

Tags:

regex

I've got the following url route and i'm wanting to make sure that a segment of the route will only accept numbers. as such, i can provide some regex which checks the word.

/page/{currentPage}

so.. can someone give me a regex which matches when the word is a number (any int) greater than 0 (ie. 1 <-> int.max).

like image 381
Pure.Krome Avatar asked Oct 31 '08 04:10

Pure.Krome


People also ask

How do I find a number in regex?

Python Regex – Get List of all Numbers from String. To get the list of all numbers in a String, use the regular expression '[0-9]+' with re. findall() method. [0-9] represents a regular expression to match a single digit in the string.

How do I find a specific word in a regular expression?

To run a “whole words only” search using a regular expression, simply place the word between two word boundaries, as we did with ‹ \bcat\b ›. The first ‹ \b › requires the ‹ c › to occur at the very start of the string, or after a nonword character.

Can you use regex with numbers?

The regex [0-9] matches single-digit numbers 0 to 9. [1-9][0-9] matches double-digit numbers 10 to 99. That's the easy part. Matching the three-digit numbers is a little more complicated, since we need to exclude numbers 256 through 999.

How do you search for a regex pattern at the beginning of a string?

The meta character “^” matches the beginning of a particular string i.e. it matches the first character of the string. For example, The expression “^\d” matches the string/line starting with a digit. The expression “^[a-z]” matches the string/line starting with a lower case alphabet.


1 Answers

/^[1-9][0-9]*$/

Problems with other answers:

/([1-9][0-9]*)/ // Will match -1 and foo1bar
#[1-9]+# // Will not match 10, same problems as the first
[1-9] // Will only match one digit, same problems as first
like image 177
eyelidlessness Avatar answered Dec 31 '22 14:12

eyelidlessness