Home > Common Problem > What are the regular expressions for integers?

What are the regular expressions for integers?

小老鼠
Release: 2023-11-14 16:11:28
Original
3120 people have browsed it

The regular expressions for integers are: 1. Match positive integers: ^[1-9]\d*$; 2. Match negative integers: ^-[1-9]\d*$; 3. Match positive and negative integers: ^-?\d $; 4. Match non-zero integers: ^(0|[1-9]\d*)$; 5. Match integers (including zero): ^-?\d $.

What are the regular expressions for integers?

Operating system for this tutorial: Windows 10 system, Dell G3 computer.

Regular Expression (Regular Expression) is a powerful text processing tool that can match, search, replace or split complex string patterns.

The following are some regular expressions that match integers (positive and negative integers):

1. Match positive integers:

^[1-9]\d*$
Copy after login

This regular expression matches any from 1 Starting at 9, followed by a string of any number of digits.

2. Match negative integers:

^-[1-9]\d*$
Copy after login

This regular expression matches any string starting with a negative sign (-), followed by a number from 1 to 9, and then followed by any number of digits .

3. Match positive and negative integers:

^-?\d+$
Copy after login
Copy after login

This regular expression matches any integer that may have a negative sign. If the string is preceded by a negative sign, the match is negative, otherwise it is positive or zero.

4. Match non-zero integers:

^(0|[1-9]\d*)$
Copy after login

This regular expression matches any non-zero integer, including positive integers and negative integers. If the string is zero, it will also be matched.

5. Match integers (including zero):

^-?\d+$
Copy after login
Copy after login

This regular expression matches any integer, including zero. If the string is preceded by a negative sign, the match is negative, otherwise it is positive or zero.

The above is the detailed content of What are the regular expressions for integers?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template