Regular match floating point number, match floating point number

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.