正文

正则表达式举例-关于数字的2008-08-17 18:05:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/iamben250/37678.html

分享到:

Expression
^#?([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?$
Description
Tests for valid HTML hexadecimal color codes. The # symbol is optional. And it will except either the 3 digit form for the 216 Web safe colors, or the full 6 digit form. I am use it on my site to allow users to customize the site's colors.
Matches
#00ccff | #039 | ffffcc
Non-Matches
blue | 0x000000 | #ff000

 

Expression
\b(([01]?\d?\d|2[0-4]\d|25[0-5])\.){3}([01]?\d?\d|2[0-4]\d|25[0-5])\b
Description
Most Concise RegExp for matching Decimal IPs. If nothing else, it'll make your code easier to read. (And I know that \d?\d is \d{1,2} but that's 2 extra characters.) --Update: darkone noticed 8 characters could be shaved down. I've edited it to reflect this. Thanks, darkone!
Matches
217.6.9.89 | 0.0.0.0 | 255.255.255.255
Non-Matches
256.0.0.0 | 0978.3.3.3 | 65.4t.54.3

Expression
^\d*\.?((25)|(50)|(5)|(75)|(0)|(00))?$
Description
This is a pattern to search and verify that a decimal number ends with a 25, 50, 75, 0 or 00. It does match for a nothing after decimal also but I guess thats ok !!
Matches
0.25 | .75 | 123.50
Non-Matches
.77 | 1.435

Expression
^\d{0,2}(\.\d{1,2})?$
Description
This regular expression validates that the data entered is a number with a maximum of two integers and two decimals and a minimum of one integer or one decimal.
Matches
99.99 | 99 | .99
Non-Matches
999.999 | 999 | .999

Expression
^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$
Description
This will grep for a valid MAC address , with colons seperating octets. It will ignore strings too short or long, or with invalid characters. It will accept mixed case hexadecimal. Use extended grep.
Matches
01:23:45:67:89:ab | 01:23:45:67:89:AB | fE:dC:bA:98:76:54
Non-Matches
01:23:45:67:89:ab:cd | 01:23:45:67:89:Az | 01:23:45:56:

Expression
(?!^0*$)(?!^0*\.0*$)^\d{1,5}(\.\d{1,3})?$
Description
This regular expression validates a number NOT 0, with no more than 5 places ahead and 3 places behind the decimal point.
Matches
1 | 12345.123 | 0.5
Non-Matches
0 | 0.0 | 123456.1234

Expression
^[+-]?([0-9]*\.?[0-9]+|[0-9]+\.?[0-9]*)([eE][+-]?[0-9]+)?$
Description
A regular expression that matches numbers. Integers or decimal numbers with or without the exponential form.
Matches
23 | -17.e23 | +.23e+2
Non-Matches
+.e2 | 23.17.5 | 10e2.0

Expression
^\d*[0-9](|.\d*[0-9]|,\d*[0-9])?$
Description
This is permit all decimal number, exclude all alphanumeric caracter
Matches
123456.123456 | 123456,123456 | 123456
Non-Matches
123a.123 | 123a,123 | a

Expression
(^\d*\.?\d*[1-9]+\d*$)|(^[1-9]+\d*\.\d*$)
Description
Accepts only positive decimal values. Zero and negatvie numbers are non-matching. Allows zeros after last non-zero numeric value after decimal place for significant digits.
Matches
0.050 | 5.0000 | 5000
Non-Matches
0 | 0.0 | .0

Expression
^\s*(?'num'\d+(\.\d+)?)\s*(?'unit'((w(eek)?)|(wk)|(d(ay)?)|(h(our)?)|(hr))s?)(\s*$)
Description
Validates Microsoft Project-type duration entries. Accepts a number and a unit. The number part can be integer or decimal. The unit can be several variations of weeks, days, and hours: e.g., w, wk, week, ws, wks, weeks are all valid. Whitespace between the number and the unit is optional: e.g., 1d, 2 days, 3.5w are all valid. Captures the number value in a group named num and the unit string in a group named 'unit'.
Matches
1 day | 3.5 w | 6hrs
Non-Matches
1 | 6. days | 1 week 2 d

Expression
[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}
Description
Very Simple Match for dotted Decimal IP address. Doesn’t Validate addresses
Matches
192.168.1.1 | 10.2.234.1 | 66.129.71.122
Non-Matches
192.168.1 | 10.2.1234.1 | 66 129 71 122

Expression
^\s*(((\d*\.?\d*[0-9]+\d*)|([0-9]+\d*\.\d*) )\s*[xX]\s*){2}((\d*\.?\d*[0-9]+\d*)|([0-9]+\d*\.\d*))\s*$
Description
This validates Length times Width times Height measurements, which consists of 3 whole or decimal numbers separated by an x.
Matches
1.1 x 4.35 x 5.0 | 1 x 2 x 3 | 4.75 x 300.25 x 0
Non-Matches
z.56 x 6 x 7 | 1 xx 2 x 3 | 1 by 2 by 3

Expression
^[-]?([1-9]{1}[0-9]{0,}(\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|\.[0-9]{1,2})$
Description
This regular expression will match on a real / decimal / floating point / numeric string with no more than 2 digits past the decimal. The negative sign (-) is allowed. No leading zeroes or commas. It is based on a currency regular expression by Tom Persing.
Matches
123 | 123.54 | -.54
Non-Matches
123.543 | 0012 | 1,000.12

Expression
^\d+(?:\.\d{0,2})?$
Description
Matches positive whole numbers with exactly zero or two decimal points if a . is present. Useful for checking currency amounts, such 5 or 5.00 or 5.25.
Matches
1 | 1.23 | 1234.45
Non-Matches
a1.34 | 1.23a | a

Expression
^(?:(?:25[0-5]|2[0-4]\d|[01]\d\d|\d?\d)(?(?=\.?\d)\.)){4}$
Description
Regular expression for validating a decimal IP address. Matches 4 groups of from 1 to 3 digits, where each group of digits ranges from 0 to 255 in value. Groups of digits must be separated by a single period (.) with no other formatting characters present. Uses conditional regex with lookahead syntax to prevent a match on a period following the final group of digits.
Matches
217.6.9.89 | 0.0.0.0 | 255.255.255.255
Non-Matches
256.0.0.0 | 0127.3 | 217.6.9.89.

Expression
(?n:(^\$?(?!0,?\d)\d{1,3}(?=(?<1>,)|(?<1>))(\k<1>\d{3})*(\.\d\d)?)$)
Description
Regular expression for validating a US currency string field. Matches an unlimited number of digits to the left of an optional decimal point. Digits to the left of the decimal point can optionally be formatted with commas, in standard US currency format. If the decimal point is present, it must be followed by exactly two digits to the right. Matches an optional preceding dollar sign. Uses regex lookahead to preclude leading zeros and to match the optional formatting comma.
Matches
$3,023,123.34 | 9,876 | 123456.78
Non-Matches
0.002 | $01.00 | ###1.00

Expression
^(((\d{1,3})(,\d{3})*)|(\d+))(.\d+)?$
Description
validates numbers, with or without decimal places, and comma 1000 separators.
Matches
9999999 | 99999.99999 | 99,999,999.9999
Non-Matches
9999. | 9,99,99999.999 | 999.9999.9999

Expression
(^N/A$)|(^[-]?(\d+)(\.\d{0,3})?$)|(^[-]?(\d{1,3},(\d{3},)*\d{3}(\.\d{1,3})?|\d{1,3}(\.\d{1,3})?)$)
Description
This pattern matches a decimal value with up to 3 digits after the decimal. Comma is allowed as a thousands separator but not required. N/A is also allowed.
Matches
405.234 | 50 | 213123.456 | -1 | N/A
Non-Matches
bathreader | this is N/A | 3.14159 | +10

Expression
^[0-9]*[1-9]+$|^[1-9]+[0-9]*$
Description
This Expression checks if the value is an integer, positive, not zero and not a decimal. Very handy for use when people need to fill in whole numbers, like when ordering car parts on a website (you dont want your customers to order -10 tires or 0.7 mirrors..
Matches
1 | 00000428123 | 1230000
Non-Matches
0 | 00000000 | any text or +, - or any other character

Expression
^(\-)?\d*(\.\d+)?$
Description
Matches all positive decimal floating negative/non-negative numbers. Allows empty string.
Matches
0.55 | 21232.00 | -89.20
Non-Matches
asdf | +0.33

Expression
(?=([\W]*[\w][\W]*\b))\s(?=\d\.|\d\b)
Description
This RegExp matches a space (&quot; &quot;) character with lookahead condition if there is an ASCII text in front of it and it is followed by a single decimal number which in turn is followed by a dot or nothing. It's useful to seperate scientific notation numbers from a text, i.e. when classifying with the bow toolkit. [EDIT 18.09.2004] There was indeed an error in the second lookahead. Changed |\b to |\d\b
Matches
ROOT 4.873624764e-34 | `1234567 890-= 3.8765e-34543 | ~! @ # $ % ^ &amp; ( % )_+ 3.345e-2384754
Non-Matches
rstuvwxyz 754.234e-23 | yz754.234e-23 | yz .234e-23

Expression
^\s*-?(\d*\.)?([0-2])?[0-9]:([0-5])?[0-9]:([0-5])?[0-9](\.[0-9]{1,7})?\s*$
Description
This should be the pattern described in the documentation for the .NET TimeSpan.Parse method - generally parses time spans. From the .NET docs: public static TimeSpan Parse(string s); The s parameter contains a specification of the form: [ws][-][d.]hh:mm:ss[.ff][ws] Items in square brackets ([ and ]) are optional, colons and periods (: and .) are literal characters, and other items are as follows. Item Description ws optional white space &quot;-&quot; optional minus sign indicating a negative time &quot;d&quot; optional days &quot;hh&quot; hours, ranging from 0 to 23 &quot;mm&quot; minutes, ranging from 0 to 59 &quot;ss&quot; seconds, ranging from 0 to 59 &quot;ff&quot; optional fractional seconds, from 1 to 7 decimal digits
Matches
10:12:34 | 932323.9:00:32.3420

Expression
(?:(?:(?<Feet>\d+)[ ]*\'){0,1}[ ]*(?<WholeInches>\d*(?![/\w])){0,1}(?:[ ,\-]){0,1}(?<Fraction>\d*\/\d*){0,1}(?<Decimal>\.\d*){0,1}\")|(?:(?<Feet>\d+)[ ]*\'[ ]*){1}
Description
I needed an expression to extract measurements from text to extract steel sizes from product descriptions, and I came up with this. It will only match on measurements that have complete dimensions (i.e. measurements with foot and inch marks in the positions you'd expect them). My personal experience required that I also be a little lax and allow measurements such as: .125 (for wall thicknesses and sheet metal thicknesses) with no inch marks. You can accomplish this by including: |(?&lt;Decimal&gt;\.\d{3}\&quot;*) to the end of the expression Hope it helps you.
Matches
1/4" x 2.125" Flat 44W x 20'3.5" | 1/8" x 4" C-1018 flat x 14' 5-1/4"
Non-Matches
44W | 8 @ 21 W.F. beam | 1/4 x 2.125 Flat 44W x 20

Expression
^\s*(\d{0,2})(\.?(\d*))?\s*\%?\s*$
Description
Basically this matches into variables for percentages.. It allows as much whitespace before and after the expression. $1 = Percent before decimal, $2 = percentage after decimal inc decimal, $3 = percentage after decimal. Disallowed anything past 99.99999 repeating percent. No negative percents either. Easy to implement but I do not need it.
Matches
10.5%, 1%, 1, .5, .0555%
Non-Matches
100%, -1%, -1, 200, 200.1

Expression
^[-+]?(\d?\d?\d?,?)?(\d{3}\,?)*(\.?\d+)$
Description
This pattern matches decimal values. It supports option leading positive or negative symbols. Supports commas where only the leading group can have less than three numbers.
Matches
+1.0 | -3 | 1,234.00 | 1,234,567
Non-Matches
A | 1,23 | 1,23,456

Expression
^[\+\-]?[0-9]+([\,\.][0-9]+)?$
Description
Accepts integer and decimal numbers, the decimal delimiter beeing either a point or a comma, prefixed or not by a plus or minus sign.
Matches
«1» «-1» «+1» «1.0» «-1.0» «+1.0» «1,0» «-1,0» «+1,0»
Non-Matches
«abc» «1e2»

Expression
^\$?(([1-9],)?([0-9]{3},){0,3}[0-9]{3}|[0-9]{0,16})(\.[0-9]{0,3})?$
Description
Currency expression, accepts 4 commas and 4 groups of 3 numbers and 1 number before the first comma, this first number will have to be different from zero. It accepts a number of, two or three decimal. It accepts zero numbers after the point. You can change the number of groups and numbers accespts before and after the point.
Matches
1234.23 | 1,234.245 | 1.
Non-Matches
12,12,0.00 | 0,123.99

Expression
^[0-9]%?$|^1[0-9]%?$|^2[0-9]%?$|^3[0-5]%?$|^[0-9]\.\d{1,2}%?$|^1[0-9]\.\d{1,2}%?$|^2[0-9]\.\d{1,2}%?$|^3[0-4]\.\d{1,2}%?$|^35%?$
Description
A range of numbers 0-35 optionally including a percent sign and 2 position decimal.
Matches
32.34%, 32.34, 32, 32%
Non-Matches
32.345%, 36, .34

Expression
^([-+]?(\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-2]?\d{1,2})?[r]?|[-+]?((\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-2]?\d{1,2})?)?[i]|[-+]?(\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-2]?\d{1,2})?[r]?[-+]((\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-2]?\d{1,2})?)?[i])$
Description
DESCRIPTION Parses a complex number of kind 'a+bi' from an input string. Please remove all spaces from the input string before using this regex pattern. MATCHING EXAMPLES '[]' means is an optional parameter; '|' means OR; '+' is the positive sign; '-' is the negative sign; '#' is one ore more decimal digits; 'E|e' are the valid exponent symbols; '...' is the range for the exponent; 'r' means the real part of complex number; 'i' means the imaginary part of complex number. NOTE Has the imaginary part of the input string not a numeric value (e.g. '5-i' is a valid format) it should be interpreted as '5-1i'!
Matches
[+|-]#[.[#]]|[#].#[E|e[+|-]0...299][r] -OR- [+|-][#[.[#]]|[#].#[E|e[+|-]0...299]]i -OR- [+|-]#[.[#]]|[#].#[E|e[+|-]0...299][r]+|-[#[.[#]]|[#].#[E|e[+|-]0...299]]i
Non-Matches
[+|-][.][E|e[+|-][0...299]][r] -OR- [+|-].[E|e[+|-][0...299]]i -OR- [+|-][.][E|e[+|-][0...299]][r]+|-[#[.[#]]|[#].#[E|e[+|-][0...299]]]i -OR- any number with more than one sign or decimal seperator -OR- any string with non-leading signs on mantissa and on exponent

Title Test Details Floating Number
Expression
^([-+]?(\d+\.?\d*|\d*\.?\d+))$
Description
DESCRIPTION Parses a floating number (such as double or float) from an input string. Please remove all spaces from the input string before using this regex pattern. MATCHING EXAMPLES The following symbols are used: '[]' means optional parameter; '|' means OR; '+' is the positive sign; '-' is the negative sign; '#' means one or more decimal digits; '.' is decimal seperator.
Matches
[+|-]#[.[#]]|[#].#
Non-Matches
[+|-][.] -OR- any floating number with an exponent (e.g. 1.0E-8) -OR- any string with more than one sign or decimal seperator -OR- any string with non-leading sign

Title Test Details Floating Number With Exponent
Expression
^([-+]?(\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-2]?\d{1,2})?)$
Description
DESCRIPTION Parses a floating number with an optional exponent from an input string. Please remove all spaces from the input string before using this regex pattern. MATCHING EXAMPLES The following symbols are used: '[]' means an optional parameter; '|' means OR; '+' means the positive sign; '-' means the negative sign; '#' means one or more numbers (0...9); '.' is the decimal seperator; 'E|e' are the valid exponent symbols; '...' is the range of values for the exponent.
Matches
[+|-]#[.[#]]|[#].#[E|e[+|-]0...299]
Non-Matches
[+|-][.][E|e[+|-][0...299]] -OR- [+|-]#[.[#]]|[#].#E|e[+|-]300...∞ -OR- any string with more than one sign on mantissa or on exponent -OR- any string with non-leading signs on mantissa or exponent -OR- any string with more than one decimal seperator on mantissa | any string with one or more decimal seperators on exponent

Title Test Details All valid Guid(s)
Expression
[({]?(0x)?[0-9a-fA-F]{8}([-,]?(0x)?[0-9a-fA-F]{4}){2}((-?[0-9a-fA-F]{4}-?[0-9a-fA-F]{12})|(,\{0x[0-9a-fA-F]{2}(,0x[0-9a-fA-F]{2}){7}\}))[)}]?
Description
It matches all strings that the .NET Framework API "new Guid(string guid)" can recognize. A String that contains a GUID in one of the following formats ('d' represents a hexadecimal digit whose case is ignored):
Matches
ca761232ed4211cebacd00aa0057b223 | CA761232-ED42-11CE-BACD-00AA0057B223 | {CA761232-ED42-11CE-BACD-00AA0057B223} | (CA761232-ED42-11CE-BACD-00AA0057B223) | {0xCA761232,0xED42,0x11CE,{0xBA,0xCD,0x00,0xAA,0x00,0x57,0xB2,0x23}}
Non-Matches
Invalid guids

Title Test Details Colon-delimited string of positive integers and/or decimal numbers
Expression
^(([1-9][0-9]*)|((([0])|([1-9][0-9]*))\.[0-9]+)|((([1-9][0-9]*)|((([0])|([1-9][0-9]*))\.[0-9]+))\:)*(([1-9][0-9]*)|((([0])|([1-9][0-9]*))\.[0-9]+)))$
Description
Accepts a colon-delimited string of positive integers and/or decimal numbers in any combination. Spaces are not permitted. Decimal numbers of less than 1 must be prefixed with a zero (e.g. accepts 0.1, but not .1), and numbers with a trailing decimal point are not accepted (e.g. accepts 3.0, but not 3.). A lone zero is not accpeted (e.g. accepts 1.02:3:4.5, but not 1.02:0:4.5). Developed from a similar expression by Steven Smith on this site.
Matches
1.2 | 0.1:0.5:56:6.70 | 3:6.78954:1:2:3
Non-Matches
0.1:.5:56 | -6.70:3. | 5: 0.1:0

Title Test Details Currency with two decimals
Expression
^\$\d{1,3}(,?\d{3})*(\.\d{2})?$
Description
Looks for a number which can be optionally comma separated at thousands and may or may not have two decimal places. Tweaking: 1. Replace the \$ symbol with your currency. 2. Toggle , and . as separators (Some European countries follow this convention) 3. Mandate comma separation by removing the ? after ,
Matches
$333,444,444,555.99
Non-Matches
$333,444,444,55

Title Test Details HTML HEX Characters codes
Expression
&#x((?=.*[ABCDEF]))*((?=.*[0-9]))*.{2,5};
Description
This pattern matches any HTML character code in hexadecimal format. Doesn't match numerical code such as &amp;#203; or any friendly code character such as &amp;nbsp;
Matches
&amp;#x1DE; &amp;#x111; &amp;#xCA;
Non-Matches
&amp;#203; &amp;Ecirc;

Title Test Details Metric and Time Dimensions
Expression
(?<Nbr>[\+-]?((\d*\,\d+)|(\d*\.\d+)|\d+))\s*(?<Unit>mm|cm|dm|min|km|s|m|h)
Description
Matches decimal numbers (english or german writing) followed by metric or time units (mm, cm, dm, m, km, s, min and h) in labels surrounded by any digit.
Matches
345,3m, 345.m, 345m, 345,5 m

Title Test Details Decimal Number
Expression
^([1-9]([0-9])?)(\.(([0])?|([1-9])?|[1]([0-1])?)?)?$
Description
Expression to match decimal number with 2 digits.
Matches
1.0, 1.1 .. 1.11 ... 99.0, 99.1, 99.2, ... 99.11
Non-Matches
0, 0.0. 0.11, 99.12, 1.12, 100, 100.11

Title Test Details Feet-inch to Decimal
Expression
(?:(?:(?<Feet>\d+)[ ]*(?:'|ft)){0,1}[ ]*(?<Inches>\d*(?![/\w])){0,1}(?:[ ,\-]){0,1}(?<Fraction>(?<FracNum>\d*)\/(?<FracDem>\d*)){0,1}(?<Decimal>\.\d*){0,1}(?:\x22| in))|(?:(?<Feet>\d+)[ ]*(?:'|ft)[ ]*){1}
Description
I needed an expression that would break down a written measurement, like 12' 2-15/16", into groups of feet, inches, fractional inches (num/dem). This is a modified expression based on Trevor Braun's orginal regex. Added the "ft" & "in" suffixes and the fraction's Numerator & Denominator groups.
Matches
1ft 2-3/4 in, 2' 3 4/5", 3ft, 4', 5 in, 6", 7.125 in, 3ft 4.5 in

Title Test Details Decimals with optional positive/negative sign, miles separator (,) and exponential notation
Expression
/^[-+]?((\d*|((\d{1,3})?,(\d{3},)*(\d{3})))?)(\.\d*)?([eE][-+]\d+)?$/
Description
Valids decimal numbers with optional support for: 1.- Use a positive/negative sign (123.78 -123.09 +0.123 are valid) 2.- Use a miles separator (12345.789 12,345.099 +12,345 are valid) 2.- Use a exponential notation (+0.123E+4 -12.345e-4 are valid)
Matches
123.78 -123.09 +0.123 12345.789 12,345.099 +0.123E+4
Non-Matches
56,88 12e3 09,98.99

 

阅读(2341) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册