Collates very detailed Java regular expression usage daqo

  • 2020-04-01 04:30:58
  • OfStack

This article is all about Java regular expression syntax.

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 bits: "^\d{n}$".
Can only enter at least n digits: "^\d{n,}$".
Can only enter m - n digits:. ^ \ d {m, n} $"
Only zero and non-zero starting Numbers can be entered: "^(0|[1-9][0-9]*)$".
Only positive real Numbers with two decimal places can be entered: "^[0-9]+(.[0-9]{2})? $".
Only positive real Numbers with 1~3 decimal places can be entered: "^[0-9]+(.[0-9]{1,3})? $".
Only positive non-zero integers can be entered: "^\+? [1-9] [0-9] * $".
Only non-zero negative integers can be entered: "^\-[1-9][]0-9"*$.
Only characters of length 3 can be entered: "^.{3}$".
You can only enter A string of 26 English letters: "^[a-za-z]+$".
You can only enter A string of 26 uppercase English letters: "^[a-z]+$".
You can only enter a string of 26 lowercase English letters: "^[a-z]+$".
You can only enter A string of Numbers and 26 English letters: "^[a-za-z0-9]+$".
You can only enter a string of Numbers, 26 letters, or underscores: "^\w+$".
Verify user password: "^[a-za-z]\w{5,17}$"
Verify that ^%& is present; ',; = the & # 63; $\" characters: "[^%&',;=?$\x22]+".
Can only input Chinese characters: "^[\u4e00-\u9fa5]{0,}$"
Verify Email address: "^ \ w + (\ w + / - +.]) * @ \ w + ([-] \ w +) * \. \ w + ([-] \ w +) * $".
Verify InternetURL: "^ (http:// -] [\ w + \.) + [-] \ w + (/ [\ w - / & # 63; % & amp; =] *) & # 63; $".
Verify phone number: "^(\(\d{3,4}-)|\d{3.4}-)? \d{7,8}$" correct format is: "xxx-xxxxxxx", "xxx-xxxxxxxx", "xxx-xxxxxxx", "xxx-xxxxxxxx", "XXXXXXX" and "XXXXXXXX".
Verify id number (15 or 18 digits) : "^\d{15}|\d{18}$".
Verify 12 months of the year: "^(0? [1-9]|1[0-2])$" correctly formatted as: "01" ~ "09" and "1" ~ "12".
Verify 31 days of the month: "^((0? [1-9])|((1|2)[0-9])|30|31) 01" ~ "09" and 1" ~ "31".
Regular expressions matching Chinese characters: [\u4e00-\u9fa5]
Match double byte characters (including Chinese characters) : [^\x00-\ XFF]
Application: calculate the length of a string (2 for a double-byte character, 1 for an ASCII character)
String. The prototype. Len = function () {return this. The replace (/ [^ \ x00 - \ XFF] / g, "aa"). The length; }
Regular expression matching empty line: \n[\s|]*\r
Regular expressions that match HTML tags: < (. *) > (. *) < / / (. *) > | < (. *) \ / >
Regular expression matching Spaces :(^\s*)|(\s*$)
Application: There is no trim function like vbscript in javascript, so we can use this expression to implement, as follows:


String.prototype.trim = function()
{
return this.replace(/(^s*)|(s*$)/g, "");
}
 Decomposition and transformation using regular expressions IP Address: 
 Here's how to use regular expression matching IP Address and will IP The address is converted to the corresponding value Javascript Application: 
function IP2V(ip)
{
re=/(d+).(d+).(d+).(d+)/g //A regular expression that matches an IP address
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, if the above program does not use regular expressions, but directly use the split function to split, the program is as follows:


var ip="10.100.20.168"
ip=ip.split(".")
alert("IP Values are: "+(ip[0]*255*255*255+ip[1]*255*255+ip[2]*255+ip[3]*1))

Matches the Email address of the regular expression: \ w + (\ w + / - +.]) * @ \ w + ([-] \ w +) * \ \ w + ([-] \ w +) *
Regular expression matching URL: http:// ([-] \ w + \.) + [-] \ w + (/ [\ w - / & # 63; % & amp; =] *) & # 63;

Use regular expressions to limit text fields in web forms:


 Limit Chinese input with regular expression: onkeyup="value=value.replace(/[^u4E00-u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^u4E00-u9FA5]/g,''))"
 Limit full-angle characters to regular expressions:  onkeyup="value=value.replace(/[^uFF00-uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^uFF00-uFFFF]/g,''))"
 Limit input to Numbers with regular expressions: onkeyup="value=value.replace(/[^d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^d]/g,''))"
 Limit input to Numbers and English with regular expressions: onkeyup="value=value.replace(/[W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^d]/g,''))"
<input onkeyup="value=value.replace(/[^u4E00-u9FA5w]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^u4E00-u9FA5w]/g,''))" value=" Allowed underline , Alphanumeric letters and Chinese characters ">
<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 of the input
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-u9FA5w]/g,'') || event.keyCode==189){//Regular remove symbol and underline 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=""> Alphanumeric letters and Chinese characters are allowed 
<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 />
 Multi-line text box control :<br />
<TEXTAREA rows="14"
cols="39" id="Textarea1" name="Textarea1" onpropertychange="MaxLength(this, 15)"></TEXTAREA><br />
 Control form contents can only be entered by Numbers , Chinese ....
<script>
function test() 
{
if(document.a.b.value.length>50)
{
alert(" No more than 50 A character! ");
document.a.b.focus();
return false;
}
}
</script>
<form name=a onsubmit="return test()">
<textarea name="b" cols="40" wrap="VIRTUAL" rows="6"></textarea>
<input type="submit" name="Submit" value="check">
</form>
 Only Chinese characters 
<input onkeyup="value=value.replace(/[^u4E00-u9FA5]/g,'')">
 Only 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="50" maxlength="35" class="input2" onkeyup="value=value.replace(/[W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^d]/g,''))">
 It can only be Numbers 
<script language=javascript>
function onlyNum()
{
if(!((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105)))
//Consider the number keys on the keypad
 event.returnValue=false;
}
</script>
<input onkeydown="onlyNum();">
 Only English characters and Numbers 
<input onkeyup="value=value.replace(/[W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^d]/g,''))">
 Validation for 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) -  The modified 
<script language="JavaScript1.2">
function test() {
if((a.b.value.indexOf ("sex") == 0)||(a.b.value.indexOf ("fuck") == 0)){
 alert(" Five lectures four beauty three love ");
 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>

 Restrict text fields to Numbers only 
<input onkeyup="if(event.keyCode !=37 && event.keyCode != 39) 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 both ordinary characters (for example, letters between a and z) and special characters (called "metacharacters"). A pattern describes one or more strings to match when searching for text.

Character description
\ marks the next character as a special character, text, backreference, or octal escape. For example, "n" matches the character "n". "\n" matches the newline character. The sequence "\" matches "\", and "\(" matches" (".
^ matches the starting position of the input string. If you set the Multiline property of the RegExp object, the ^ will also match the position after "\n" or "\r".
$matches the position at the end of the input string. If the Multiline property of the RegExp object is set, the $will also match the position before "\n" or "\r".
* matches the preceding character or subexpression zero or more times. For example, zo* matches "z" and "zoo". * is equivalent to {0,}.
+ matches the preceding character or subexpression once or more. For example, "zo+" matches "zo" and "zoo" but not "z". + is equivalent to {1,}.
The & # 63; Matches the preceding character or subexpression zero or once. For example, "do (es) & # 63;" Matches the "do" in "do" or "does". The & # 63; Is equivalent to {0,1}.
{n}n is a non-negative integer. It matches exactly n times. For example, "o{2}" does not match the "o" in "Bob", but it matches the two "o" in "food".
{n,}n is a non-negative integer. At least n matches. 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 and at most m times. For example, "o{1,3}" matches the first three o's in "fooooood". 'o{0,1}' is equivalent to 'o? '. Note: you cannot insert a space between a comma and a number.
The & # 63; When this character is followed by any other qualifier (*, +, ? , {n}, {n,}, {n,m}), the matching pattern is "non-greedy". The "non-greedy" pattern matches the searched string as short as possible, while the default "greedy" pattern matches the searched string as long as possible. For example, in the string "oooo", "o+?" Only a single "o" is matched, while "o+" matches all "o".
Matches any single character other than "\n". To match any character including "\n", use a pattern such as "[\s\ s]".
(pattern) matches the pattern and captures the matching subexpression. You can use $0... The $9 attribute retrieves the captured match from the resulting "match" collection. To match the parenthesized character (), use "\(" or" \) ".
(& # 63; A subexpression that matches a 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 in cases where the pattern part is combined with the "or" character (|). For example, 'industr (& # 63; :y|ies) is a more economical expression than 'industry|industries'.
(& # 63; =pattern) performs a forward prediction first search subexpression that matches the string at the beginning of the string matching 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" in "Windows 2000", but does not match "Windows" in "Windows 3.1". Prediction first does not occupy characters, that is, after a match occurs, the search for the next match follows the previous match, rather than after the character that makes up prediction first.
(& # 63; ! Pattern) performs a reverse prediction first search subexpression that matches a search string that is not 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 (? ! 'matches' Windows' in' Windows 3.1 ', but not 'Windows' in' Windows 2000 '. Prediction first does not occupy characters, that is, after a match occurs, the search for the next match follows the previous match, rather than after the character that makes up prediction first.
X |y matches either x or y. For example, 'z|food' matches 'z 'or' food'. '(z|f)ood' matches' zood 'or' food '.
The [xyz] character set. Matches any of the contained characters. For example, "[ABC]" matches "a" in "plain".
[^xyz] reverse character set. Matches any characters not included. For example, "[^ ABC]" matches "p" in "plain".
[a-z] character range. Matches any character in the specified range. For example, "[a-z]" matches any lowercase letter in the "a" to "z" range.
[^a-z] reverse range character. Matches any character that is not in the specified range. For example, "[^a-z]" matches any character that is not in the "a" to "z" range.
\b matches a word boundary, that 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 nonword boundary matching. "Er \B" matches "er" in verb, but not "er" in "never".
\cx matches the control character indicated by x. For example, \cM matches control-m or carriage return. The value of x has to be between A minus Z or A minus Z. If not, assume that c is the "c" character itself.
\d numeric character matching. It's the same thing as [0-9].
\D non-numeric character matching. It's the same thing as ^0-9.
\f page change character match. It's the same thing as \x0c and \cL.
\n newline matching. It's equivalent to \x0a and \cJ.
\r matches a carriage return. It's equivalent to \x0d and \cM.
\s matches any whitespace characters, including Spaces, tabs, page breaks, etc. Is equivalent to [\f\n\r\t\v].
\S matches any non-white space characters. Is equivalent to [^ \f\n\r\t\v].
\t TAB matching. Is equivalent to \x09 and \cI.
\v vertical TAB matching. Is equivalent to \x0b and \cK.
\w matches any character of a word class, including an underscore. 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" and "\ x04" & amp; "1" equivalent. Allows the use of ASCII code in regular expressions.
\num matches num, where num is a positive integer. To catch the matching backreference. For example, "(.)\1" matches two consecutive identical characters.
\n identifies an octal escape code or backreference. N is a backreference if the \n is preceded by at least n capture subexpression. Otherwise, if n is an octal number (0-7), then n is an octal escape code.
\nm identifies an octal escape code or backreference. Nm is a backreference if the \nm is preceded by at least nm capture subexpression. If the \nm is preceded by at least n catches, then n is a backreference followed by the character m. If neither of the preceding cases exists, \nm matches the octal value nm, where n and m are octal digits (0-7).
\ NML when n is octal (0-3) and m and l are octal (0-7), the octal escape code NML is matched.
\ UN matches n, where n is a Unicode character represented by a four-digit hexadecimal number. For example, \u00A9 matches the copyright symbol (?) .

Especially detailed, especially detailed Java regular expression syntax daquan, hope you like.


Related articles: