Need a simple RegEx to find a number in one word

I have the following URL and I want to make sure that the route segment will only accept numbers. as such, I can provide some regular expression that validates the word.

/ page / {CurrentPage}

so .. can someone give me a regular expression that matches when a word is a number (any int) is greater than 0 (i.e. 1 ↔ int.max).

+3
source share
6 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
+23
source

If you need more than 0, use this regex:

/([1-9][0-9]*)/

This will work until the number has leading zeros (for example, "03").

[0-9]+ .

+4

.

/\/page\/(0*[1-9][0-9]*)/ or "Perl-compatible" /\/page\/(0*[1-9]\d*)/

, 0. , - , .

, , , , , , ^ $. - , . , , , .

/(^|[^0-9-])(0*[1-9][0-9]*)([^0-9]|$)/

, (\b), RE . , , , , , - , , .

Perl :

/(?<![\d-])(0*[1-9]\d*)\b/

lookbehind , '-' , -1 " " "-" "1". lookbehind - "-" .

, ^ , (?<![\d-]).

+1
string testString = @"/page/100";
string pageNumber = Regex.Match(testString, "/page/([1-9][0-9]*)").Groups[1].Value;

""

0

( , ..), : (, Apache mod_rewrite), , ( ) .

: /\b([1-9][0-9]*)$/
, max int, : /\b([1-9][0-9]{0,2})$/ .

0

This will match any string, so if it contains /page/, it should be followed by a number consisting not only of zeros.

^(?!.*?/page/([0-9]*[^0-9/]|0*/))

(?! )is a negative outlook. It will match an empty string only if it contains a template that does not match the current position.

0
source

Source: https://habr.com/ru/post/1698952/


All Articles