Organize very detailed Java regular expressions using the encyclopedia _java

Source: Internet
Author: User
Tags control characters numeric lowercase numeric value pow

The entire contents of this article are organized for Java regular expression syntax and are shared with you:

[Regular expression] text box input content control
Integer or decimal: ^[0-9]+\. {0,1} [0-9] {0,2}$
Only numbers can be entered: "^[0-9]*$".
Only n digits can be entered: "^\d{n}$".
You can enter at least n digits: "^\d{n,}$".
You can enter only the number of m~n digits:. "^\d{m,n}$"
Only numbers beginning with 0 and not 0 can be entered: ^ (0|[ 1-9][0-9]*) $ ".
You can only enter positive real numbers with two decimal digits: ^[0-9]+ (. [ 0-9]{2})? $ ".
You can only enter positive real numbers with 1~3 decimal places: ^[0-9]+ (. [ 0-9]{1,3})? $ ".
You can only enter a Non-zero positive integer: "^\+?" [1-9] [0-9]*$].
You can only enter a Non-zero negative integer: "^\-[1-9][]0-9" *$.
You can only enter characters with a length of 3: "^. {3}$ ".
You can only enter a string of 26 English letters: "^[a-za-z]+$".
You can only enter a string consisting of 26 uppercase letters: "^[a-z]+$".
You can only enter a string consisting of 26 lowercase English letters: "^[a-z]+$".
You can only enter a string consisting of numbers and 26 English letters: "^[a-za-z0-9]+$".
You can only enter a string of numbers, 26 English letters, or underscores: "^\w+$".
Verify user password: "^[a-za-z]\w{5,17}$" is the correct format: start with a letter, length between 6~18, can only contain characters, numbers, and underscores.
Verify that there are ^%& ',; =?$\ ' and other characters: "[^%& ',; =?$\x22]+".
Only Chinese characters can be entered: "^[\u4e00-\u9fa5]{0,}$"
Verify email Address: "^\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *$ ".
Verify InternetURL: "^http://([\w-]+\.) +[\w-]+ (/[\w-./?%&=]*)? $ ".
Verify phone Number: "^ (\ (\d{3,4}-) |\d{3.4}-)? \d{7,8}$" The correct format is: "Xxx-xxxxxxx", "xxxx-xxxxxxxx", "xxx-xxxxxxx", "xxx-xxxxxxxx", " XXXXXXX "and" XXXXXXXX ".
Verify ID Number (15-bit or 18-digit): "^\d{15}|\d{18}$".
Verify 12 months of the year: "^" (0?[ 1-9]|1[0-2]) $ "The correct format is:" 01 "~" 09 "and" 1 "~" 12 ".
Verify one months of 31 days: "^ (0?[ 1-9]) | ((1|2) [0-9]) |30|31) $ "the correct format is;" 01 "~" 09 "and" 1 "~" 31 ".
Matching regular expressions for Chinese characters: [\U4E00-\U9FA5]
Match Double-byte characters (including Chinese characters): [^\x00-\xff]
Application: Computes the length of the string (a double-byte character length meter 2,ascii character 1)
String.prototype.len=function () {return this.replace (/[^\x00-\xff]/g, "AA"). Length;}
A regular expression that matches a blank row: \n[\s|] *\r
Regular expression matching HTML tags:< (. *) > (. *) <\/(. *) >|< (. *) \/>
Matching a regular expression with a trailing space: (^\s*) | (\s*$)
Application:There is no trim function like VBScript in JavaScript, we can use this expression to implement the following:

String.prototype.trim = function ()
{return
this.replace (^\s*) | ( \s*$)/g, "");
Using regular expressions to decompose and transform IP addresses: The
Following is a JavaScript program that uses regular expressions to match an IP address and converts an IP address to a corresponding numeric value:
function ip2v (IP)
{
re=/(\d+) \. ( \d+) \. (\d+) \. (\d+)/g//matching the regular expression of an IP address (
re.test (IP))
{return
Regexp.$1*math.pow (255,3)) +regexp.$2*math.pow ( 255,2)) +regexp.$3*255+regexp.$4*1
}
else
{
throw new Error ("not a valid IP address!")
}
}

However, if the above program does not use a regular expression, and the split function directly to decompose may be simpler, the program is as follows:

var ip= "10.100.20.168"
ip=ip.split (".")
Alert ("IP value is:" + (IP[0]*255*255*255+IP[1]*255*255+IP[2]*255+IP[3]*1))

Regular expression matching an email address: \w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *
A regular expression that matches URL URLs: http://([\w-]+\.) +[\w-]+ (/[\w-/?%&=]*)?

Use regular expressions to restrict the entry of text boxes in a Web page's form:

The regular expression limit can only be entered in Chinese: onkeyup= "value=value.replace (/[^\u4e00-\u9fa5]/g,") "Onbeforepaste=" Clipboarddata.setdata (' Text ', Clipboarddata.getdata (' text '). Replace (/[^\u4e00-\u9fa5]/g, ') "restricts only full-width characters in regular expression: onkeyup=" value= Value.replace (/[^\uff00-\uffff]/g, ') "onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text ') ). Replace (/[^\uff00-\uffff]/g, ') "can only enter a number with regular expression restrictions: onkeyup=" Value=value.replace (/[^\d]/g, ")" onbeforepaste= " Clipboarddata.setdata (' Text ', Clipboarddata.getdata (' text '). Replace (/[^\d]/g, ') "Restrict only numbers and English in regular expression: onkeyup=" Value=value.replace (/[\w]/g, ') "onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\d]/g, ")" "<input onkeyup=" Value=value.replace (/[^\u4e00-\u9fa5\w]/g, ' ")" onbeforepaste= " Clipboarddata.setdata (' Text ', Clipboarddata.getdata (' text '). Replace (/[^\u4e00-\u9fa5\w]/g, ') "value=" allows underlining, Numeric letters and Kanji "> <script language=" javascript > if (document.layers)//Triggers keyboard events document.captureevents (
event.keypress) function XZ (thsv,nob) {if (nob== "2") {Window.clipboardData.setData ("text", "") alert ("Avoid illegal character input, do not copy characters"); return false;} if (Event.keycode!=8 & amp;& event.keycode!=16 && event.keycode!=37 && event.keycode!=38 && event.keycode!=39 && event.keycode!=40) {thsvv=thsv.value;//entered value thsvs=thsvv.substring (thsvv.length-1);//The last character entered//thsvss= Thsvv.substring (0,thsvv.length-1)//Remove the last error character if (!thsvs.replace (/[^\u4e00-\u9fa5\w]/g, ') | | event.keycode==189)
{//Regular drop symbol and underscore key thsv.value= ' do not enter illegal symbols [' +thsvs+ '] '; alert (' Do not enter illegal symbols [' +thsvs+ '] '); thsv.value= ""; return false;} } </script> <input onkeyup= "XZ (this,1)" onpaste= "XZ (this,2)" value= "" > Allow numeric letters and kanji <script language= "
JavaScript "> <!--function MaxLength (field,maxlimit) {var j = field.value.replace (/[^\x00-\xff]/g," * * "). Length;
Alert (j);
var Tempstring=field.value;
var tt= ""; if (J > Maxlimit) {for (Var i=0;i<maxlimit;i++) {If tt.replace (/[^\x00-\xff]/g, "* *"). Length < Maxlimit) TT = Tempstring.substr (0,i+1);
else break;
} if (Tt.replace (/[^\x00-\xff]/g, "* *"). Length > Maxlimit) tt=tt.substr (0,tt.length-1);
Field.value = TT;
}else{}} </script> single-line text box control <br/> <input type= "text" id= "Text1" "Name=" Text1 "onpropertychange=" MaxLength (this, 5) ><br/> Multiline text box control: <br/> <textarea rows= "cols=" id= "Textarea1" Onpropertychange= "MaxLength (This)" ></textarea><br/> Control Form content can only enter numbers, Chinese ... <script> function Test () {if (document.a.b.value.length>50) {alert ("Cannot exceed 50 characters!")
");
Document.a.b.focus ();
return false; } </script> <form name=a onsubmit= "return Test ()" > <textarea name= "b" cols= "" wrap= "VIRTUAL" rows= "6" & Gt;</textarea> <input type= "Submit" name= "Submit" value= "Check" > </form> can only be kanji <input onkeyup= " Value=value.replace (/[^\u4e00-\u9fa5]/g, ') ' > can only be English characters <script language=javascript> function Onlyeng () {if (! event.keycode>=65&&event.keycode<=90)) Event.returnvaLue=false; } </script> <input onkeydown= "Onlyeng ();" > <input name= "coname" type= "text" size= "maxlength=" to "class=" Input2 "onkeyup=" Value=value.replace (/[\w]/g
, ' onbeforepaste= ' clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\d]/g, ') "> can only be a number <script language=javascript> function Onlynum () {if (!) ( (event.keycode>=48&&event.keycode<=57) | |
(event.keycode>=96&&event.keycode<=105)))
Consider the number key event.returnvalue=false on the keypad; } </script> <input onkeydown= "Onlynum ();" > can only be English characters and digital <input onkeyup= "Value=value.replace (/[\w]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\d]/g, ') "> validated as email format <script language=javascript runat=server > Function Isemail (stremail) {if Stremail.search (/^\w+ (-\w+) | ( \.\w+)) *\@[a-za-z0-9]+ (\.| -) [a-za-z0-9]+) *\.
[a-za-z0-9]+$/)!=-1) return true;
else alert ("Oh"); } </SCRIPT> <input Type=text onblur=isemail (this.value) > Shield keywords (sex, fuck)-Modified <script language= "JavaScript1.2" > Function test () {if ((A.b.value.indexof ("Sex") = 0) | | (A.b.value.indexof ("fuck") = = 0))
 {Alert ("Shime three Loves");
 A.b.focus ();
return false;} } </script> <form name=a onsubmit= "return Test ()" > <input type=text name=b> <input type= "Submit" Nam E= "Submit" value= "Check" > </form> restricted text box can only enter digital <input onkeyup= "If" (Event.keycode!=37 && Event.keycode!=) value=value.replace (/\d/g, '); " Onbeforepaste= "Clipboarddata.setdata" (' Text ', Clipboarddata.getdata (' text '). Replace (/\d/g, ') "> <pixtel_ Mmi_ebook_2005>2 </PIXTEL_MMI_EBOOK_2005>

A regular expression is a text pattern that includes ordinary characters (for example, letters between A and z) and special characters (called "metacharacters"). The pattern describes one or more strings to match when searching for text.

Character description
\ marks the next character as a special character, text, reverse reference, or octal escape character. For example, "n" matches the character "n". "\ n" matches line breaks. The sequence "\" matches "\", "\" ("Match" ().
^ matches the position at which the input string begins. If the Multiline property of the RegExp object is set, ^ will also match the position after "\ n" or "\ r".
$ matches the position of the end of the input string. If the Multiline property of the RegExp object is set, $ also matches the position before "\ n" or "\ r".
* 0 or more times to match the preceding character or subexpression. For example, zo* matches "z" and "Zoo". * is equivalent to {0,}.
+ matches the preceding character or subexpression one or more times. For example, "zo+" matches "Zo" and "Zoo", but does not match "Z". + is equivalent to {1,}.
? 0 or more times to match the preceding character or subexpression. For example, "Do (es)?" Match "Do" in "do" or "does". is equivalent to {0,1}.
{N}n is a nonnegative integer.} Matches n times exactly. For example, "o{2}" does not match "O" in "Bob", but matches two "o" in "food".
{N,}n is a nonnegative integer.} Match at least n times. For example, "o{2,}" does not match "O" in "Bob" and matches all o in "Foooood". "O{1,}" is equivalent to "o+". "O{0,}" is equivalent to "o*".
{N,m} M and n are nonnegative integers, where n <= m. Match at least n times, at most m times. For example, "o{1,3}" matches the first three o in "Fooooood". ' o{0,1} ' is equivalent to ' o '. Note: You cannot insert a space between commas and numbers.
? When this character follows any other qualifier (*, + 、?、 {n}, {n,}, {n,m}), the matching pattern is "not greedy." The "not greedy" pattern matches the shortest possible string of searches, while the default "greedy" pattern matches the search for the longest string possible. For example, in the string "Oooo", "o+?" Matches only a single "O", and "o+" matches All "O".
. matches any single character except "\ n". To match any character including "\ n", use a pattern such as "[\s\s]".
Match pattern and catch the child expression of the match. You can use the $0...$9 property to retrieve a captured match from the result "match" collection. To match the bracket character (), use "\ (" or "\)".
(?:p Attern) A subexpression that matches the pattern but does not capture the match, that is, it is a non capture match and does not store a match for later use. This is useful for combining a pattern part with an "or" character (|). For example, ' Industr (?: y|ies) is a more economical expression than ' industry|industries '.
(? =pattern) performs a subexpression of a forward lookahead search that matches the string at the starting point of the string that matches pattern. It is a non capture match, that is, a match cannot be captured for later use. For example, ' Windows (? =95|98| nt|2000) ' matches windows in Windows 2000, but does not match windows in Windows 3.1. Lookahead does not occupy characters, that is, when a match occurs, the next matched search follows the previous match, not the word end-of-file that makes up the lookahead.
(?! Pattern) performs a subexpression of a reverse lookahead search that matches a search string that is not at the starting point of a string that matches pattern. It is a non capture match, that is, a match cannot be captured for later use. For example, ' Windows (?! 95|98| nt|2000) ' matches windows in Windows 3.1, but does not match windows in Windows 2000. Lookahead does not occupy characters, that is, when a match occurs, the next matched search follows the previous match, not the word end-of-file that makes up the lookahead.
X|y matches x or Y. For example, ' Z|food ' matches ' z ' or ' food '. ' (z|f) Ood ' matches ' zood ' or ' food '.
[XYZ] Character set. Matches any one of the characters contained. For example, "[ABC]" matches "a" in "plain".
[^XYZ] Reverse character set. Matches any characters that are not included. For example, "[^abc]" matches "P" in "plain".
[A-z] character range. Matches any character within the specified range. For example, "[A-z]" matches any lowercase letter in the range "a" through "Z".
[^a-z] reverses the range character. Matches any character that is not in the specified range. For example, "[^a-z]" matches any character that is not in the range "a" through "Z".
\b Matches a word boundary, which is the position between the word and the space. For example, "er\b" matches "er" in "never", but does not match "er" in "verb".
\b is not a word boundary match. "er\b" matches "er" in "verb", but does not match "er" in "Never".
\CX matches the control characters indicated by X. For example, \cm matches a control-m or carriage return character. The value of x must be between A-Z or a-Z. If this is not the case, then C is assumed to be the "C" character itself.
\d numeric character matching. equivalent to [0-9].
\d non-numeric character matching. is equivalent to [^0-9].
\f a page break match. Equivalent to \x0c and \CL.
\ n line break match. Equivalent to \x0a and \CJ.
\ r matches a carriage return character. Equivalent to \x0d and \cm.
\s matches any white space character, including spaces, tabs, page breaks, and so on. is equivalent to [\f\n\r\t\v].
\s matches any non-white-space character. is equivalent to [^ \f\n\r\t\v].
\ t tab match. is equivalent to \x09 and \ci.
\v vertical tab matching. is equivalent to \x0b and \ck.
\w matches any word class character, including underscores. is equivalent to "[a-za-z0-9_]".
\w matches any non word character. is equivalent to "[^a-za-z0-9_]".
\XN matches N, where N is a hexadecimal escape code. The hexadecimal escape code must be exactly two digits long. For example, "\x41" matches "A". "\x041" is equivalent to "\x04" & "1". Allows ASCII code to be used in regular expressions.
\num matches num, where num is a positive integer. The reverse reference to the capture match. For example, "(.) \1 "matches two consecutive identical characters.
\ n identifies a octal escape code or a reverse reference. N is a reverse reference if there are at least n catch subexpression in front. Otherwise, if n is the octal number (0-7), then N is the octal escape code.
\NM identifies a octal escape code or a reverse reference. NM is a reverse reference if there are at least nm catch subexpression in front of the \nm. If there are at least N captures in front of the \nm, then n is the reverse reference followed by the character M. If neither of the preceding conditions exists, the \NM matches the octal value nm, where N and M are octal digits (0-7).
\NML matches octal escape code NML when n is a octal number (0-3), and M and L are octal numbers (0-7).
\un matches N, where N is a Unicode character in four-bit hexadecimal numbers. For example, \u00a9 matches the copyright symbol (?).

Especially detailed, detailed Java regular expression Grammar encyclopedia, I hope you like.

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.