正则表达式截取URL参数值
生活随笔
收集整理的這篇文章主要介紹了
正则表达式截取URL参数值
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
java 代碼
import java.util.regex.Matcher; import java.util.regex.Pattern;public class Test2 {public static void main(String[] args) {System.out.println(getMatcherStr("abc=def=ghi=gk", ".*=")); //abc=def=ghi=// 當?字符緊跟在任何一個其他限制符 (*, +, ?, {n}, {n,}, {n,m}) 后面時,匹配模式是非貪婪的。// 非貪婪模式盡可能少的匹配所搜索的字符串,而默認的貪婪模式則盡可能多的匹配所搜索的字符串。System.out.println(getMatcherStr("abc=def=ghi=gk", ".*?=")); //abc=System.out.println(getMatcherStr1("abc=def=ghi=gk", "(.*)=")); //abc=def=ghi// 非貪婪模式System.out.println(getMatcherStr1("abc=def=ghi=gk", "(.*?)=")); //abcString url = "http://mp.weixin.qq.com/s?__biz=MzA4NDI3NjcyNA==&mid=2649510587&idx=1&sn=1ef899baa603919ed02b383a73f6fb8b&chksm=87f15620b086df3693479efd6f6a1f3391e2e825799ea8b85029bc20cf8f343ad65d2921cc93&scene=38#wechat_redirect";System.out.println(getMatcherStr1(url, "biz=(.+?)&")); //MzA4NDI3NjcyNA==System.out.println(getMatcherStr1(url, "mid=(.+?)&")); //2649510587System.out.println(getMatcherStr1(url, "idx=(.+?)&")); //1}private static String getMatcherStr(String content, String sPattern) {Pattern p = Pattern.compile(sPattern);Matcher m = p.matcher(content);if(m.find()){return m.group();}return null;}private static String getMatcherStr1(String content, String sPattern) {Pattern p = Pattern.compile(sPattern);Matcher m = p.matcher(content);if(m.find()){return m.group(1);}return null;}}總結
以上是生活随笔為你收集整理的正则表达式截取URL参数值的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Appium 与 Chromedrive
- 下一篇: FileZilla Server : F