Evaluate a regular expression and another regular expression or test method. (Not enough !)

Source: Internet
Author: User
Evaluate a regular expression and another regular expression or test method. (Not enough !) Regular Expression number

The younger brother asks God to give a regular expression and another regular expression or test method, the specific situation is:

1. there is For such input, the user is required to enter the date in the format of 9: 08,14: 54. Note that the hours can be one or two digits.
In this case, how do I write a regular expression? And how to use this regular expression to determine? (The younger brother has long known that regular expressions are amazing, but they have never been used. please be careful)


2. another input is The user is required to enter the amount of money in the format of 13.04; 5000; 123 ..... In short, the input number can be decimal or integer, and the decimal point can be one or two digits, but it must be within two digits. In a word, I want to judge whether the user entered a number or float with a decimal point here, because I will add these numbers later.

I think, is there a magic regular expression that can be directly detected? Or, if you just click it, you just need to judge whether the input is a number?
Please give a magic regular expression or a test method.

This post has a score of 100, which is not enough for me.


Reply to discussion (solution)

Write a test amount first

//$money = '123';//$money = '12.3';//$money = '123.4';$money = '123.45';//$money = '123.456';//$money = '123456';if( preg_match('/^\d+(\.\d{1,2})?$/',$money) ){echo $money;}

If we strictly test the time and use regular expressions to write data during the test, it will be a lot of trouble.
It is not a matter of hour or two, but it is not easy to detect hours exceeding the valid range of 36.
If (preg_match ('/([0-9] | 0 [0-9] | 1 [0-9] | 2 [0-3]): [0-5] [0-9]/', $ time )){
Echo $ time;
}

Thank you. I will test it immediately.

The test is successful. thanks and paste it.

If we strictly test the time and use regular expressions to write data during the test, it will be a lot of trouble.
It is not a matter of hour or two, but it is not easy to detect hours exceeding the valid range of 36.
If (preg_match ('/([0-9] | 0 [0-9] | 1 [0-9] | 2 [0-3]): [0-5] [0-9]/', $ time )){
Echo $ time;
}

Thank you! score delivery.

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.