Regular expressions that match Java double values __ regular expressions

Source: Internet
Author: User
Tags perl script

Recently saw the Java Master Zi Zhu wrote the "Judge whether the number is a double regular expression", I have also been studying the application of regular expression, so to play. This is written in Perl script because it supports regular expressions better. Let's do a step-by-step solution double floating point number: normal integers are valid double, with the suffix letter D or D also double. For example: 1 +10-100 100d 987D so use regular expressions:

^[-+]?/d[dd]$
to match. A variable of type Double can also hold a real number. For example: 1.0 +12.34-45.896-563.887d even 124. .123-.687 is also a valid double value in Java. 1 if there are integers before the decimal point, the decimal and decimal points can be arbitrary; 2 If there is no number before the decimal point, the default is to 0 as an integer, but the decimal must appear. That is, there is only one decimal point, and that is the illegal double number. So we're going to do this with a multiple-selection branch:
^[-+]? (/d+ (/./d*)? | /./d+) [dd]?$
There is also a double variable, with scientific notation. The exponent portion can only be a decimal integer and is allowed to be negative. For example: 1e123 12.546e54d 4335.546E33-.54e-7d so you have to add an exponential match at the tail. Indices do not have decimals, so you can just match integers.
^[-+]? (/d+ (/./d*)? | /./d+) ([ee][-+]?/d+)? [dd]?$
Finally, and most troublesome, is the range of values in Java double: negative range: from -1.7976931348623157x10+308 to -4.94065645841246544x10-324 Positive range: from 4.94065645841246544x10-324 to 1.7976931348623157x10+308 that is to say, the index range is between 324 and 308. If that's the case, it's okay to say that the key is that the decimal is still so complicated. To simplify the problem, I put the index range between 323 and 307, and there is no limit to the decimal number in front. That first matches-307 to 307.
[-+]? ([012]?/d{1,2}|30[0-7])
Then the other in the match-308 to-324
-3 ([01]?[ 4-9]| [012]? [0-3])
Combined, that is:
^[-+]? (/d+ (/./d*)? | /./d+) ([EE] ([-+]? ( [012]?/d{1,2}|30[0-7]) |-3 ([01]?[ 4-9]| [012]? [0-3]))? [dd]?$

Use this regular expression to write a Perl script:

#!/usr/bin/perl while

($line = <STDIN>)
{
	chomp ($line);
	if ($line =~/^[-+]? ( /d+ (/./d*)? | (/./d+)) ([EE] [-+]? ( [012]?/d{1,2}|30[0-7]) |-3 ([01]?[ 4-9]| [012]? [0-3]))? [dd]?$/)
	{
		print $line, "is Java double!/n";
	}
	else
	{
		print $line, "is not Java double/n";
	}
}

Test data:

+
-
.
E
a
1.
1
0.1
-.
-.1
+.1
1e123d
-3.543e4456d
-45.54879e-323d
-777.1234E-324
4553.876e357
1543e307
13.54e15.78d
213.e123d
1.2.3
-.1.1
+e
E12

Output

+ is not Java double-are not Java double. are not Java Double e are not
java double
A are not Java Doub Le
1 is Java double!
. 1 is Java double!
0.1 is Java double!
-. Is isn't Java double
-.1 is Java double!
+.1 is Java double!
1e123d is Java double!
-3.543E4456D is isn't Java double
-45.54879e-323d is Java double!
-777.1234E-324 is isn't Java double
4553.876e357 is isn't Java double
1543e307 is Java double!
13.54E15.78D is isn't Java double
213.e123d is Java double!
1.2.3 is not Java double
-.1.1 are not Java double +e are not Java
Double E12 are not
Java double
Copyright Notice

I all original articles are reserved copyright, please respect original works.
Reprint must include this statement, keep this article complete, and in the form of hyperlinks to the original author "Redraiment" and the main site of this article's original address. Contact Way

My mailbox, welcome letter (redraiment@gmail.com)
My blogger (Zi Qing line)
My Google Sites (zi Qing line)
My csdn blog (Dream Ting Xuan)
My Baidu space (Dream Ting Xuan)

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.