Several practical regular expressions

Positive integers between 1 and 100 regular

Expression: ^[1-9]\d?$|^100$

explain:

^ indicates the starting position of the matching string

[1-9] represents any one of the numbers 1-9

\d means any number

? Represents 0 or 1 occurrence of the preceding character or subexpression

$ indicates the end position of the matching string

| means or

The final interpretation is: match a string that satisfies the following conditions:

Start with a number from 1-9 and can be followed by 0 or 1 number, or 100


Positive integers between 20 and 100 regular

Expression: ^([2-9]\d|[1-9]\d{2})(?<=99|00)$

explain:

^ Indicates the start position of the matched string

() represents a capture group for extracting matched content

[2-9] represents any one of the numbers 2-9

\d means any number

| means or

[1-9]\d{2} means a three-digit number starting with the digits 1-9 followed by two arbitrary digits

(?<=99|00) indicates that it ends with 99 or 00, using a look-ahead assertion

$ indicates the end position of the matched string

The final interpretation is: match a string that satisfies the following conditions:

Start with a number in the number 2-9, followed by any number, and the number range is between 20-99; or start with a number 1-9, followed by two numbers, and the number range is between 100-999 and end with 99 or 00.


Regex to keep 2 decimal places

Regular expression: ^[0-9]+(.[0-9]{1,2})?$

explain:

^ Indicates the beginning of the matched string

[0-9]+ means match at least one digit

(.[0-9]{1,2})? Indicates an optional decimal part, where . means to match the decimal point, and [0-9]{1,2} means to match the decimal part of one or two digits

$ indicates the end position of the matched string

The final interpretation is: match a number or a decimal, the decimal has at most two decimal parts, and the decimal point and the integer part are not necessary.


Guess you like

Origin blog.csdn.net/q6115759/article/details/130421257