捕获 | |
---|---|
(exp) | 匹配exp,并捕获文本到自动命名的组里 |
(?<name>exp) | 匹配exp,并捕获文本到名称为name的组里 |
(?:exp) | 匹配exp,不捕获匹配的文本 |
位置指定 | |
(?=exp) | 匹配exp前面的位置 |
(?<=exp) | 匹配exp后面的位置 |
(?!exp) | 匹配后面跟的不是exp的位置 |
(?<!exp) | 匹配前面不是exp的位置 |
注释 | |
(?#comment) | 这种类型的组不对正则表达式的处理产生任何影响,只是为了提供让人阅读注释 |
示例数据:
String addr="季楓115<abc@163.com>"
获取:abc@163.com
public static String sendMaillAddr(String str){ String sendMaillAddrule="<(.*?)>"; Pattern sendMaillAddrPattern=Pattern.compile(sendMaillAddrule); Matcher matcher = sendMaillAddrPattern.matcher(str); while(matcher.find()){ return matcher.group(1); } return null; }
直接上示例:
我们现在要匹配html的input的value值内容(淘宝登陆表单为例):
<input name='_tb_token_' type='hidden' value='3fe3ae393739e'>
<input type="hidden" name="action" value="Authenticator" />
<input type="hidden" name="event_submit_do_login" value="anything" />
<input type="hidden" name="TPL_redirect_url" value="" />
<input type="hidden" name="from" value="tb" />
<input type="hidden" name="fc" value="2" />
<input type="hidden" name="style" value="default" />
<input type="hidden" name="css_style" value="" />
<input type="hidden" name="tid" />
<input type="hidden" name="support" value="000001"/>
<input type="hidden" name="CtrlVersion" value="1,0,0,7"/>
<input type="hidden" name="loginType" value="3" />
<input type="hidden" name="minititle" value=""/>
<input type="hidden" name="minipara" value=""/>
<input type="hidden" name="pstrong" value="" />
<input type="hidden" name="longLogin" id="longLogin1" value="0" />
<input type="hidden" name="llnick" id="llnick1" value="" />
<input type="hidden" name="sign" value=""/>
<input type="hidden" name="need_sign" value=""/>
<input type="hidden" name="isIgnore" value=""/>
<input type="hidden" name="popid" value=""/>
<input type="hidden" name="callback" value=""/>
<input type="hidden" name="guf" value=""/>
<input type="hidden" name="not_duplite_str" value=""/>
<input type="hidden" name="need_user_id" value=""/>
<input type="hidden" name="poy"/>
<input type="hidden" id="gvfdc" name="gvfdcname" value=""/>
<input type="hidden" name="from_encoding" value=""/>
<input type="hidden" name="action" value="Authenticator" />
<input type="hidden" name="event_submit_do_login" value="anything" />
<input type="hidden" name="TPL_redirect_url" value="" />
<input type="hidden" name="from" value="tb" />
<input type="hidden" name="fc" value="2" />
<input type="hidden" name="style" value="default" />
<input type="hidden" name="css_style" value="" />
<input type="hidden" name="tid" />
<input type="hidden" name="support" value="000001"/>
<input type="hidden" name="CtrlVersion" value="1,0,0,7"/>
<input type="hidden" name="loginType" value="3" />
<input type="hidden" name="minititle" value=""/>
<input type="hidden" name="minipara" value=""/>
<input type="hidden" name="pstrong" value="" />
<input type="hidden" name="longLogin" id="longLogin1" value="0" />
<input type="hidden" name="llnick" id="llnick1" value="" />
<input type="hidden" name="sign" value=""/>
<input type="hidden" name="need_sign" value=""/>
<input type="hidden" name="isIgnore" value=""/>
<input type="hidden" name="popid" value=""/>
<input type="hidden" name="callback" value=""/>
<input type="hidden" name="guf" value=""/>
<input type="hidden" name="not_duplite_str" value=""/>
<input type="hidden" name="need_user_id" value=""/>
<input type="hidden" name="poy"/>
<input type="hidden" id="gvfdc" name="gvfdcname" value=""/>
<input type="hidden" name="from_encoding" value=""/>
如第一个input我们的匹配结果希望是:3fe3ae393739e
直接上java代码
……
vector=GetValue(vector.firstElement(),"<input",">",1);
……
/// <summary>
/// 获得字符串中开始和结束字符串中间得值
/// </summary>
/// <param name="str">字符串</param>
/// <param name="s">开始</param>
/// <param name="e">结束</param>
///<param name="azimuth">匹配内容是否包含开始与结束符号,0:不包含 1:包含</param>
/// <returns></returns>
public static Vector<String> GetValue(String str, String s, String e,int azimuth){
Pattern pattern;
if (azimuth==1){
pattern = Pattern.compile("(?=(" + s + "))[.\\s\\S]*?(?<=(" + e + "))");
}else{
pattern = Pattern.compile("(?<=(" + s + "))[.\\s\\S]*?(?=(" + e + "))");
}
Matcher matcher = pattern.matcher(str);
StringBuffer buffer = new StringBuffer();
Vector<String> vector=new Vector<String>();
while(matcher.find()){
vector.add(matcher.group());
}
return vector;
}
vector=GetValue(vector.firstElement(),"<input",">",1);
……
/// <summary>
/// 获得字符串中开始和结束字符串中间得值
/// </summary>
/// <param name="str">字符串</param>
/// <param name="s">开始</param>
/// <param name="e">结束</param>
///<param name="azimuth">匹配内容是否包含开始与结束符号,0:不包含 1:包含</param>
/// <returns></returns>
public static Vector<String> GetValue(String str, String s, String e,int azimuth){
Pattern pattern;
if (azimuth==1){
pattern = Pattern.compile("(?=(" + s + "))[.\\s\\S]*?(?<=(" + e + "))");
}else{
pattern = Pattern.compile("(?<=(" + s + "))[.\\s\\S]*?(?=(" + e + "))");
}
Matcher matcher = pattern.matcher(str);
StringBuffer buffer = new StringBuffer();
Vector<String> vector=new Vector<String>();
while(matcher.find()){
vector.add(matcher.group());
}
return vector;
}