12、Java 正则表达式

'^''$'

"^The""The""There""The cat"
"of despair$""of despair"
"^abc$""abc""abc"
"notice""notice"

使

'*''+''?'

"ab*"ab"a", "ab", "abbb",
"ab+"ab
"ab?"ab
"a?b+$"ab

使

"ab{2}"a2b"abb"
"ab{2,}"a2b
"ab{3,5}"a35b

"{0,2}""{,2}"'*''+''?'"{0,}""{1,}""{0,1}"'¦'

"hi¦hello""hi""hello"
"(b¦cd)ef""bef""cdef"
"(a¦b)*c""a""b""c"

'.'

"a.[0-9]""a"
"^.{3}$"3

"[ab]""a""b""a¦b"
"[a-d]"'a''d'"a¦b¦c¦d""[abcd]"
"^[a-zA-Z]"
"[0-9]%"
",[a-zA-Z0-9]$"

'^''^'"%a-zA-Z%"

"^.$()¦*+?{"''

 

1.

<input onkeyup="value=value.replace(/[\W]/g,'') " onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))" ID="Text1" NAME="Text1">

2.

<input onkeyup="value=value.replace(/[^\d]/g,'') " onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))" ID="Text2" NAME="Text2">

3.

<input onkeyup="value=value.replace(/[^\uFF00-\uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\uFF00-\uFFFF]/g,''))" ID="Text3" NAME="Text3">

4.

<input onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))" ID="Text4" NAME="Text4">

***************************************

//
/^[0-9]{1,20}$/

^ ^
$ $
[ ] 
[0-9] 0-9
{1,20}120[0-9]120

/^  $/使

***************************************

//5-20_.
/^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}$/

^[a-zA-Z]{1} 
([a-zA-Z0-9]|[._]){4,19} 419[._]

***************************************

//1-30
/^[a-zA-Z]{1,30}$/

***************************************

//6-20线
/^(\w){6,20}$/

\w线

***************************************

//+-   
/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/

\d09
?

+123 -999 999 +123-999 999 123 999 999 +123 999999

***************************************

//URL
/^http[s]{0,1}:\/\/.+$/  /^http[s]{0,1}:\/\/.{1,n}$/ (urllength(https://) + n )

\ / /
. 
+ {1,}1

***************************************

//
/^[\u4E00-\u9FA5]+$/

[\u4E00-\u9FA5] 

javascript

<html>
<script language="JavaScript">
<!--
function regx(r,s)
{
      if (r == null || r == ""){
            return false;
      }
      var patrn= new RegExp(r);
      if (patrn.exec(s))
            return true
      return false
}

-->
</script>
<body>
<form>
       <input type="input" name="regxStr" value="" > (/ /)
<br>
       <input type="input" name="str" value="" >

      <input type="button" name="match" value="" onClick="alert(regx(regxStr.value,str.value));">
</form>
</body>
</html>

"^\d+$"  // + 0
"^[0-9]*[1-9][0-9]*$"  //
"^((-\d+)|(0+))$"  // + 0
"^-[0-9]*[1-9][0-9]*$"  //
"^-?\d+$"    //
"^\d+(\.\d+)?$"  // + 0
"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$"  //
"^((-\d+(\.\d+)?)|(0+(\.0+)?))$"  // + 0
"^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$"  //
"^(-?\d+)(\.\d+)?$"  //
"^[A-Za-z]+$"  //26
"^[A-Z]+$"  //26
"^[a-z]+$"  //26
"^[A-Za-z0-9]+$"  //26
"^\w+$"  //26线
"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$"    //email
"^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$"  //url
/^(d{2}|d{4})-((0([1-9]{1}))|(1[1|2]))-(([0-2]([1-9]{1}))|(3[0|1]))$/   // --
/^((0([1-9]{1}))|(1[1|2]))/(([0-2]([1-9]{1}))|(3[0|1]))/(d{2}|d{4})$/   // //
"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$"   //Emil
"(d+-)?(d{4}-?d{7}|d{3}-?d{8}|^d{7,8})(-d+)?"     //
"^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$"   //IP
^([0-9A-F]{2})(-[0-9A-F]{2}){5}$   //MAC
^[-+]?\d+(\.\d+)?$ //

javascript

//
function isDigit(s)
{
var patrn=/^[0-9]{1,20}$/;
if (!patrn.exec(s)) return false
return true
}
//5-20_.
function isRegisterUserName(s)
{
var patrn=/^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}$/;
if (!patrn.exec(s)) return false
return true
}
//1-30
function isTrueName(s)
{
var patrn=/^[a-zA-Z]{1,30}$/;
if (!patrn.exec(s)) return false
return true
}
//6-20线
function isPasswd(s)
{
var patrn=/^(\w){6,20}$/;
if (!patrn.exec(s)) return false
return true
}
//+-
function isTel(s)
{
//var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?(\d){1,12})+$/;
var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;
if (!patrn.exec(s)) return false
return true
}
//-
function isMobil(s)
{
var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;
if (!patrn.exec(s)) return false
return true
}
//
function isPostalCode(s)
{
//var patrn=/^[a-zA-Z0-9]{3,12}$/;
var patrn=/^[a-zA-Z0-9 ]{3,12}$/;
if (!patrn.exec(s)) return false
return true
}
"^\\d+$"  // + 0
"^[0-9]*[1-9][0-9]*$"  //
"^((-\\d+)|(0+))$"  // + 0
"^-[0-9]*[1-9][0-9]*$"  //
"^-?\\d+$"    //
"^\\d+(\\.\\d+)?$"  // + 0
"^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$"  //
"^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$"  // + 0
"^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$"  //
"^(-?\\d+)(\\.\\d+)?$"  //
"^[A-Za-z]+$"  //26
"^[A-Z]+$"  //26
"^[a-z]+$"  //26
"^[A-Za-z0-9]+$"  //26
"^\\w+$"  //26线
"^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$"    //email
"^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$"  //url

JAVA

JAVA


1-Email

 

public static void main(String[] args) {
  // 
  String str = "service@xsoftlab.net";
  // 
  String regEx = "[a-zA-Z_]{1,}[0-9]{0,}@(([a-zA-z0-9]-*){1,}\\.){1,3}[a-zA-z\\-]{1,}";
  // 
  Pattern pattern = Pattern.compile(regEx);
  // 
  // Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
  Matcher matcher = pattern.matcher(str);
  // 
  boolean rs = matcher.matches();
  System.out.println(rs);
}

2

public static void main(String[] args) {
  // 
  String str = "baike.xsoftlab.net";
  // 
  String regEx = "baike.*";
  // 
  Pattern pattern = Pattern.compile(regEx);
  // 
  // Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
  Matcher matcher = pattern.matcher(str);
  // /
  boolean rs = matcher.find();
  System.out.println(rs);
}

3

[]
^[0-9]+\.{0,1}[0-9]{0,2}$
"^[0-9]*$"
n"^\d{n}$"
n"^\d{n,}$"
m~n"^\d{m,n}$"
"^(0|[1-9][0-9]*)$"
"^[0-9]+(.[0-9]{2})?$"
1~3"^[0-9]+(.[0-9]{1,3})?$"
"^\+?[1-9][0-9]*$"
"^\-[1-9][]0-9"*$
3"^.{3}$"
26"^[A-Za-z]+$"
26"^[A-Z]+$"
26"^[a-z]+$"
26"^[A-Za-z0-9]+$"
26线"^\w+$"
"^[a-zA-Z]\w{5,17}$"6~18线
^%&',;=?$\""[^%&',;=?$\x22]+"
"^[\u4e00-\u9fa5]{0,}$"
Email"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"
InternetURL"^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$"
"^(\(\d{3,4}-)|\d{3.4}-)?\d{7,8}$""XXX-XXXXXXX""XXXX-XXXXXXXX""XXX-XXXXXXX""XXX-XXXXXXXX""XXXXXXX""XXXXXXXX"
1518"^\d{15}|\d{18}$"
12"^(0?[1-9]|1[0-2])$""01""09""1""12"
31"^((0?[1-9])|((1|2)[0-9])|30|31)$""01""09""1""31"
[\u4e00-\u9fa5]
()[^\x00-\xff]
2ASCII1
String.prototype.len=function(){return this.replace(/[^\x00-\xff]/g,"aa").length;}
\n[\s| ]*\r
html<(.*)>(.*)<\/(.*)>|<(.*)\/>
(^\s*)|(\s*$)
javascriptvbscripttrim
String.prototype.trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
IP
IPIPJavascript
function IP2V(ip)
{
re=/(\d+)\.(\d+)\.(\d+)\.(\d+)/g //IP
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!")
}
}
split
var ip="10.100.20.168"
ip=ip.split(".")
alert("IP"+(ip[0]*255*255*255+ip[1]*255*255+ip[2]*255+ip[3]*1))
Email\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
URLhttp://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?


onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))"
onkeyup="value=value.replace(/[^\uFF00-\uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\uFF00-\uFFFF]/g,''))"
onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"
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="线,">
<script language="javascript">
if (document.layers)//
document.captureEvents(Event.KEYPRESS)
function xz(thsv,nob){
if(nob=="2"){
window.clipboardData.setData("text","")
alert(",");
return false;
}
if (event.keyCode!=8 && event.keyCode!=16 && event.keyCode!=37 && event.keyCode!=38 && event.keyCode!=39 && event.keyCode!=40){
thsvv=thsv.value;//
thsvs=thsvv.substring(thsvv.length-1);//
//thsvss=thsvv.substring(0,thsvv.length-1);//
if (!thsvs.replace(/[^\u4E00-\u9FA5\w]/g,'') || event.keyCode==189){//线 key
thsv.value=' ['+thsvs+']';
alert(' ['+thsvs+']');
thsv.value="";
return false;
}
}
}
</script>
<input onkeyup="xz(this,1)" onPaste="xz(this,2)" value="">
<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>
<br />
<INPUT type="text" id="Text1" name="Text1" onpropertychange="MaxLength(this, 5)"><br />
:<br />
<TEXTAREA rows="14"
cols="39" id="Textarea1" name="Textarea1" onpropertychange="MaxLength(this, 15)"></TEXTAREA><br />
,....
<script>
function test()  
{
if(document.a.b.value.length>50)
{
alert("50");
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>

<input onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')">

<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,''))">

<script language=javascript>
function onlyNum()
{
if(!((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105)))
//
  event.returnValue=false;
}
</script>
<input onkeydown="onlyNum();">

<input onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))">
email
<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)>
(sex , fuck) - 
<script language="JavaScript1.2">
function test() {
if((a.b.value.indexOf ("sex") == 0)||(a.b.value.indexOf ("fuck") == 0)){
  alert("");
  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>


<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>

JAVA


a  z 



 
/^\s*$/


/\d{2}-\d{5}/
 5  ID 

/<\s*(\S+)(\s[^>]*)?>[\s\S]*<\s*\/\1\s*>/
 HTML 




 
\
nn\n\\\\((

^
 RegExp  Multiline ^ \n\r

$
 RegExp  Multiline $ \n\r

*
zo* zzoo*  {0,}

+
zo+zozooz+  {1,}

?
do(es)?dodoesdo?  {0,1}

{n}
n  n o{2}Bobofoodo

{n,}
n  n o{2,}Bobofoooood oo{1,}o+o{0,}o*

{n,m}
M  n  n <= m n  m o{1,3}fooooood o'o{0,1}'  'o?'

?
*+?{n}{n,}{n,m}ooooo+?oo+o

.
\n\n使[\s\S]

(pattern)
 pattern 使 $0$9  ( )使\(\)

(?:pattern)
 pattern 使or (|) 'industr(?:y|ies)  'industry|industries' 

(?=pattern)
 pattern 使'Windows (?=95|98|NT|2000)' Windows 2000WindowsWindows 3.1Windows

(?!pattern)
 pattern 使'Windows (?!95|98|NT|2000)' Windows 3.1 WindowsWindows 2000Windows

x|y
 x  y'z|food' zfood'(z|f)ood' zoodfood

[xyz]
[abc]plaina

[^xyz]
[^abc]plainp

[a-z]
[a-z]az

[^a-z]
[^a-z]az

\b
er\bnevererverber

\B
er\Bverberneverer

\cx
 x \cM  Control-M x  A-Z  a-z  c c

\d
 [0-9]

\D
 [^0-9]

\f
 \x0c  \cL

\n
 \x0a  \cJ

\r
 \x0d  \cM

\s
 [ \f\n\r\t\v] 

\S
 [^ \f\n\r\t\v] 

\t
 \x09  \cI 

\v
 \x0b  \cK 

\w
线[A-Za-z0-9_]

\W
[^A-Za-z0-9_]

\xn
 n n \x41A\x041\x04&1使 ASCII 

\num
 num num (.)\1

\n
 \n  n  n  n  (0-7) n 

\nm
 \nm  nm  nm  \nm  n  n  m \nm  nm n  m  (0-7)

\nml
 n  (0-3)m  l  (0-7)  nml

\un
 n n  Unicode \u00A9  (?)

JAVA

 

1

  
^[\u0391-\uFFE5]+$
^[1-9]\d{5}$
QQ ^[1-9]\d{4,10}$
^[a-zA-Z_]{1,}[0-9]{0,}@(([a-zA-z0-9]-*){1,}.){1,3}[a-zA-z-]{1,}$
 + //线 ^A-Za-z+$
^13|4|5|8\d{8}$
URL ^((http|https)://)?([\w-]+.)+[\w-]+(/[\w-./?%&=]*)?$
18 ^(\d{6})(18|19|20)?(\d{2})([01]\d)([0123]\d)(\d{3})(\d|X|x)?$

 

2

\ \n\n\n\\((
^ RegExpMultiline^\n\r
$ RegExpMultiline$\n\r
* zozzozoo{0,}
+ (1zo+zozooz+{1,}
? do(es)?dodoesdo?{0,1}
{n} nno{2}Bobofoodo
{n,} nno{2,}Bobofooooodoo{1,}o+o{0,}o*
{n,m} mnn<=mnmo{1,3}foooooodoo{0,1}o?
? *,+,?{n}{n,}{n,m}ooooo+?oo+o
. \r\n\r\n使[\s\S]
(pattern) patternMatchesVBScript使SubMatchesJScript使$0$9使()
(?:pattern) pattern使使(|)industr(?:y|ies)industry|industries
(?=pattern) pattern使Windows(?=95|98|NT|2000)Windows2000WindowsWindows3.1Windows
(?!pattern) pattern使Windows(?!95|98|NT|2000)Windows3.1WindowsWindows2000Windows
(?<=pattern) (?<=95|98|NT|2000)Windows2000WindowsWindows3.1WindowsWindows
(?<!pattern) (?<!95|98|NT|2000)Windows3.1WindowsWindows2000WindowsWindows
x|y xyz|foodzfood"zood"()(z|f)oodzoodfood
[xyz] [abc]plaina
[^xyz] abcplainplin
[a-z] [a-z]az:,,; ,.
[^a-z] a-zaz
\b \ber\bnevererverber
\B er\Bverberneverer
\cx x\cMControl-MxA-Za-zcc
\d [0-9]
\D 0-9
\f \x0c\cL
\n \x0a\cJ
\r \x0d\cM
\s [ \f\n\r\t\v]
\S  \f\n\r\t\v
\t \x09\cI
\v \x0b\cK
\w 线[A-Za-z0-9_]""使Unicode
\W A-Za-z0-9_
\xn nn\x41A\x041\x04&1使ASCII
\num numnum(.)\1
\n \nnnn0-7n
\nm \nmnmnm\nmnnmnm0-7\nmnm
\nml n0-7ml0-7nml
\un nnUnicode\u00A9©
< > word<><the>"for the wise""the""otherwise""the"
( )  (  ) group9 \1 \9 
| Or(him|her) "it belongs to him""it belongs to her""it belongs to them."
+ 19+999999
? 01
{i} {i,j} A[0-9]{3} "A"3A123A348A1234[0-9]{4,6} 456

 

 

 

posted @ 2020-07-20 18:12  发哥讲Java  阅读(216)  评论(0编辑  收藏  举报