programing

"0을 제외한 모든 양의 정수"의 정규식은 무엇입니까?

goodcopy 2022. 9. 25. 23:05
반응형

"0을 제외한 모든 양의 정수"의 정규식은 무엇입니까?

어떻게 할 수 있지?^\d+$허용하지 않도록 개선되다0?

편집(구체화):

허용할 예:
1
30
111
허용하지 않는 예:
0
00
-22

선행 0이 있는 양수가 허용되는지 여부는 중요하지 않습니다(예:022).

Java JDK Regex 구현용입니다.

이것을 시험해 보세요.

^[1-9]\d*$

30자를 초과하는 패딩도 있습니다.SO 응답 제한:-)

데모입니다.

늦게 와서 미안한데 작전본부에서 허락하고 싶대076하지만 아마 이 모든 것을0000000000.

따라서 이 경우 0이 아닌1개 이상의 숫자를 포함하는 문자열이 필요합니다.그것은

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

부정적인 예측 주장을 시도할 수 있습니다.

^(?!0+$)\d+$

이것을 사용해 보세요.요건을 충족시키기 위해 이것이 가장 효과적입니다.

[1-9][0-9]*

다음은 샘플 출력입니다.

String 0 matches regex: false
String 1 matches regex: true
String 2 matches regex: true
String 3 matches regex: true
String 4 matches regex: true
String 5 matches regex: true
String 6 matches regex: true
String 7 matches regex: true
String 8 matches regex: true
String 9 matches regex: true
String 10 matches regex: true
String 11 matches regex: true
String 12 matches regex: true
String 13 matches regex: true
String 14 matches regex: true
String 15 matches regex: true
String 16 matches regex: true
String 999 matches regex: true
String 2654 matches regex: true
String 25633 matches regex: true
String 254444 matches regex: true
String 0.1 matches regex: false
String 0.2 matches regex: false
String 0.3 matches regex: false
String -1 matches regex: false
String -2 matches regex: false
String -5 matches regex: false
String -6 matches regex: false
String -6.8 matches regex: false
String -9 matches regex: false
String -54 matches regex: false
String -29 matches regex: false
String 1000 matches regex: true
String 100000 matches regex: true

^\d*[1-9]\d*$

전면에 0으로 패딩된 경우에도 모든 양의 값을 포함할 수 있습니다.

허용한다

1

01

10

11 등

허락하지 않다

0

00

000 등

0을 제외한 임의의 양의 정수:^\+?[1-9]\d*$
0을 포함한 임의의 양의 정수:^(0|\+?[1-9]\d*)$

이거 있어요.

^[1-9]|[0-9]{2,}$

누가 이겼어요?:)

재미삼아 룩헤드를 사용한 또 다른 대안:

^(?=\d*[1-9])\d+$

원하는 숫자의 숫자입니다만, 적어도 하나는[1-9].

필요할 수 있습니다(편집: 폼의 allow number of the form).0123):

^\\+?[1-9]$|^\\+?\d+$

하지만 나라면 대신 할 것이다.

int x = Integer.parseInt(s)
if (x > 0) {...}

이 RegEx는 0 중 임의의 정수 양수와 일치합니다.

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

두 개의 음의 뒷부분과 함께 작동하며, 숫자 앞에 마이너스(-)를 검색하여 음의 숫자 앞에 마이너스(-).또한 -9보다 큰 음수(예: -22)에도 적용됩니다.

패턴은 복잡하지만 정확히 "0을 제외한 임의의 양의 정수"(1 - 2147483647, 길지 않음)를 포함합니다.10진수용이고 선행 0은 사용할 수 없습니다.

^((1?[1-9][0-9]{0,8})|20[0-9]{8}|(21[0-3][0-9]{7})|(214[0-6][0-9]{6})
|(2147[0-3][0-9]{5})|(21474[0-7][0-9]{4})|(214748[0-2][0-9]{3})
|(2147483[0-5][0-9]{2})|(21474836[0-3][0-9])|(214748364[0-7]))$

^[1-9]*$는 제가 생각할 수 있는 가장 간단한 것입니다.

이것은 소수점 >0 만을 허용해야 합니다.

^([0-9]\.\d+)|([1-9]\d*\.?\d*)$

언급URL : https://stackoverflow.com/questions/7036324/what-is-the-regex-for-any-positive-integer-excluding-0

반응형