Regular match floating point number, match floating point number
Recently, I need to write a regular expression to match a decimal number (non-negative decimal number, retain two digits). In order to facilitate study with everyone, I will write down the regular expression analysis process.
First, the analysis will list all valid and illegal characters that can be matched. Valid values include 1, 1.11, 0.12, 0.3, 0, and 0.00. Illegal values may include 001, 01.1, 01.,-1, and 1.123.
Second, the data based on the above features is roughly divided into two types: decimal or integer. We consider decimal places and integer digits separately. If the integer is not 0, the first digit must not be 0, therefore, we can first write the regular expression ([1-9] \ d *) of this Part to match non-0. If 0 is taken into account, add or link, and add the ^ limit, it can be written as ^ ([1-9] \ d *) | 0 ). Match the decimal part first ., the regular expression is \., limit one to two decimal places \ d {1-2}, and connect \. \ d {1-2}. This part is optional at the same time, so it must be included and added ?, Change to (\. \ d {1-2 })? $, The final synthesis is ^ ([1-9] \ d *) | 0) (\. \ d {1-2 })? $
Third, test the regular expression in the tool and test the data listed in step 1 to see if it meets expectations.
Regular Expression Learning