Java Regular expression "Daquan"

Source: Internet
Author: User
Tags pow

[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}$".
Only numbers with at least n digits can be entered: "^\d{n,}$".
You can enter only the digits of the m~n bit:. "^\d{m,n}$"
Only numbers starting with 0 and non-0 can be entered: "^ (0|[ 1-9][0-9]*) $ ".
You can only enter a positive real number with two decimal places: "^[0-9]+ (. [ 0-9]{2})? $ ".
You can only enter a positive real number with a decimal position: "^[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" *$.
Only characters with a length of 3 can be entered: "^. {3}$ ".
You can only enter a string consisting of 26 English letters: "^[a-za-z]+$".
You can only enter a string consisting of 26 uppercase English 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 a number and 26 English letters: "^[a-za-z0-9]+$".
You can only enter a string consisting of a number, 26 letters, or underscores: "^\w+$".
Verify user password: "^[a-za-z]\w{5,17}$" is in the correct format: start with a letter, length between 6~18, can contain only characters, numbers, and underscores.
Verify that it contains ^%& ',; =?$\ ' 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 the 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 the Social Security number (15-bit or 18-digit number): "^\d{15}|\d{18}$".
Validation 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) $ "correct format for;" 01 "~" 09 "and" 1 "~" 31 ".
Regular expressions that match Chinese characters: [\U4E00-\U9FA5]
Match double-byte characters (including kanji): [^\x00-\xff]
Application: Calculates the length of a string (a double-byte character length meter 2,ascii character 1)
String.prototype.len=function () {return this.replace (/[^\x00-\xff]/g, "AA"). Length;
Regular expression that matches a blank line: \n[\s|] *\r
Regular expressions Matching HTML tags:< (. *) > (. *) <\/(. *) >|< (. *) \/>
Regular expression matching the leading and trailing spaces: (^\s*) | (\s*$)
Application: There is no trim function like VBScript in JavaScript, and we can use this expression to do the following:
String.prototype.trim = function ()
{
Return This.replace (/(^\s*) | ( \s*$)/g, "");
}
Use regular expressions to decompose and convert IP addresses:
The following is a JavaScript program that matches an IP address with a regular expression and translates an IP address into a corresponding value:
function IP2V (IP)
{
re=/(\d+) \. (\d+) \. (\d+) \. (\d+)/g//matching regular expressions for IP addresses
if (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, the above program without regular expression, and directly with the split function decomposition may be more simple, 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 email address: \w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *
Regular expression matching URL URL:/http ([\w-]+\.) +[\w-]+ (/[\w-./?%&=]*)?

Use regular expressions to restrict the entry of text boxes in Web Forms:
Use regular expression restrictions to enter only Chinese: onkeyup= "value=value.replace (/[^\u4e00-\u9fa5]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\u4e00-\u9fa5]/g, ') "
Restrict only full-width characters with regular expressions: onkeyup= "Value=value.replace (/[^\uff00-\uffff]/g,") "Onbeforepaste=" Clipboarddata.setdata (' Text ', Clipboarddata.getdata (' text '). Replace (/[^\uff00-\uffff]/g, ')) "
Use regular expression restrictions to enter only numbers: onkeyup= "Value=value.replace (/[^\d]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\d]/g, ')) "
Use regular expression restrictions to enter only numbers and English: 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=" allow underscore, alphanumeric and kanji ">
<script language= "JavaScript" >
if (document.layers)//Trigger keyboard event
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 && event.keycode!=16 && event.keycode!=37 && event.keycode!=38 && event.keycode!=39 && event.keycode!=40) {
thsvv=thsv.value;//the value entered
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 alphanumeric 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= "14"
cols= "id=" Textarea1 "name=" Textarea1 "onpropertychange=" MaxLength (This,) "></textarea><br/>
Control the contents of the form can only enter numbers, Chinese ....
<script>
function test ()
{
if (DOCUMENT.A.B.VALUE.LENGTH&GT;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" ></textarea>
<input type= "Submit" name= "Submit" value= "Check" >
</form>
Can only be Chinese characters
<input onkeyup= "Value=value.replace (/[^\u4e00-\u9fa5]/g, ')" >
can only be English characters
<script language=javascript>
function Onlyeng ()
{
if (! ( EVENT.KEYCODE&GT;=65&AMP;&AMP;EVENT.KEYCODE&LT;=90))
Event.returnvalue=false;
}
</script>
<input onkeydown= "Onlyeng (); >
<input name= "Coname" type= "text" size= "" maxlength= "+" 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 numeric keys on the keypad
Event.returnvalue=false;
}
</script>
<input onkeydown= "Onlynum (); >
can only be English characters and numbers
<input onkeyup= "Value=value.replace (/[\w]/g, ')" onbeforepaste= "Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\d]/g, ')) ">
Verify to 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) >
Mask keyword (sex, fuck)-Modified
<script language= "JavaScript1.2" >
function Test () {
if ((A.b.value.indexof ("sex") = = 0) | | (A.b.value.indexof ("fuck") = = 0)) {
Alert ("Four Graces and three Passions");
A.b.focus ();
return false;}
}
</script>
<form name=a onsubmit= "return Test ()" >
<input Type=text name=b>
<input type= "Submit" name= "Submit" value= "Check" >
</form>

You can only enter numbers in the Limit text box
<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>

Java Regular expression syntax (GO)
Regular expression syntax

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

Regular expression Examples

Expression Matching
/^\s*$/
Matches a blank line.

/\d{2}-\d{5}/
Validates an ID number consisting of two digits, a hyphen, plus 5 digits.

/<\s* (\s+) (\s[^>]*) >[\s\s]*<\s*\/\1\s*>/
Matches the HTML tag.

The following table contains a complete list of metacharacters and their behavior in the context of a regular expression:


Character description
\
Marks the next character as a special character, text, reverse reference, or octal escape. For example, "n" matches the character "n". "\ n" matches the line break. The sequence "\ \" matches "\", "\ (" Match "(".

^
Matches the starting position of the input string. 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 you set the Multiline property of the RegExp object, the $ will also match the position before \ n or \ r.

*
Matches the preceding character or sub-expression 0 or more times. For example, zo* matches "z" and "Zoo". * Equivalent to {0,}.

+
Matches the preceding character or sub-expression one or more times. For example, "zo+" matches "Zo" and "Zoo", but does not match "Z". + equivalent to {1,}.

?
Matches the preceding character or sub-expression 0 or one time. For example, "Do (es)?" Match "Do" in "do" or "does".? Equivalent to {0,1}.

N
N is a non-negative integer. Matches exactly n times. For example, "o{2}" does not match "O" in "Bob", but matches two "o" in "food".

{N,}
N is a non-negative integer. Match at least n times. For example, "o{2,}" does not match "O" in "Bob", but matches all o in "Foooood". "O{1,}" is equivalent to "o+". "O{0,}" is equivalent to "o*".

{N,m}
M and n are non-negative integers, where n <= m. Matches at least n times, up to 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 a comma and a number.

?
When this character follows any other qualifier (*, + 、?、 {n}, {n,}, {n,m}), the matching pattern is "non-greedy". The "non-greedy" pattern matches the shortest possible string searched, while the default "greedy" pattern matches the string that is searched for as long as possible. For example, in the string "Oooo", "o+?" Only a single "O" is matched, and "o+" matches All "O".

.
Matches any single character except "\ n". To match any character that includes "\ n", use a pattern such as "[\s\s]".

(pattern)
Matches the pattern and captures the matched sub-expression. 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-capturing match and does not store a match for later use. This is useful for combining pattern parts with the "or" character (|). For example, ' Industr (?: y|ies) is a more economical expression than ' industry|industries '.

(? =pattern)
A subexpression that performs a forward lookahead search that matches the string at the starting point of the string that matches the pattern. It is a non-capture match, that is, a match that cannot be captured for later use. For example, ' Windows (? =95|98| nt|2000) ' Matches Windows 2000 ' in Windows, but does not match Windows 3.1 in Windows. Lookahead does not occupy characters, that is, when a match occurs, the next matching search immediately follows the previous match, rather than the word specifier that makes up the lookahead.

(?! Pattern
A subexpression that performs a reverse lookahead search that matches a search string that is not at the starting point of a string that matches the pattern. It is a non-capture match, that is, a match that cannot be captured for later use. For example, ' Windows (?! 95|98| nt|2000) ' matches Windows 3.1 ' in Windows, but does not match Windows 2000 in Windows. Lookahead does not occupy characters, that is, when a match occurs, the next matching search immediately follows the previous match, rather than the word specifier that makes up the lookahead.

X|y
Match x or Y. For example, ' Z|food ' matches ' z ' or ' food '. ' (z|f) Ood ' matches "Zood" or "food".

[XYZ]
Character. Matches any one of the characters contained. For example, "[ABC]" matches "a" in "plain".

[^XYZ]
The reverse character set. Matches any characters that are not contained. For example, "[^abc]" matches "P" in "plain".

[A-z]
The character range. Matches any character within the specified range. For example, "[A-z]" matches any lowercase letter in the range "a" to "Z".

[^a-z]
The inverse 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" to "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
Non-word boundary match. "er\b" matches "er" in "verb", but does not match "er" in "Never".

\cx
Matches the control character 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 the C is assumed to be the "C" character itself.

\d
numeric character matching. equivalent to [0-9].

\d
Non-numeric character matching. equivalent to [^0-9].

\f
The page break matches. Equivalent to \x0c and \CL.

\ n
Line break matches. Equivalent to \x0a and \CJ.

\ r
Matches a carriage return character. Equivalent to \x0d and \cm.

\s
Matches any whitespace character, including spaces, tabs, page breaks, and so on. equivalent to [\f\n\r\t\v].

\s
Matches any non-whitespace character. equivalent to [^ \f\n\r\t\v].

\ t
TAB matches. Equivalent to \x09 and \ci.

\v
Vertical tab matches. Equivalent to \x0b and \ck.

\w
Matches any character, including underscores. Equivalent to "[a-za-z0-9_]".

\w
Matches any non-word character. Equivalent to "[^a-za-z0-9_]".

\xn
Match 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 the use of ASCII code in regular expressions.

\num
Matches num, where num is a positive integer. To capture a matching reverse reference. For example, "(.) \1 "matches two consecutive identical characters.

\ n
Identifies an octal escape code or a reverse reference. If there are at least N captured subexpression in front of it, then N is a reverse reference. Otherwise, if n is an octal number (0-7), then N is the octal escape code.

\nm
Identifies an octal escape code or a reverse reference. If there is at least a NM capture subexpression in front of the \nm, then NM is a reverse reference. If there are at least N captures in front of the \nm, then n is a reverse reference followed by the character M. If neither of the preceding cases exists, then \nm matches the octal value nm, where N and M are octal digits (0-7).

\nml
When n is an octal number (0-3), M and L are octal numbers (0-7), the octal escape code NML is matched.

\un
Matches n, where N is a Unicode character represented by a four-bit hexadecimal number. For example, \u00a9 matches the copyright symbol (?).

Java Regular expression "Daquan"

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.