正则表达式解析url参数

解析url参数正则:(?<=\?|&)[\w\={},:‘‘""]*(?<=[^#])

正好项目中要用到 捣鼓了好久还是不会.最终放弃使用split分割的方式解析发现好落伍

  public static NameValueCollection QueryString(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return null;
            }
          var arr=path.Split(new char[] { ‘?‘ }, StringSplitOptions.RemoveEmptyEntries);
          if (arr.Length != 2)
          {
              return null;
          }
          var values = arr[1];
          arr = values.Split(new char[] { ‘&‘ }, StringSplitOptions.RemoveEmptyEntries);
          if (arr == null) return null;
          string[] itemvalues;
          NameValueCollection nvcs = new NameValueCollection();
          foreach (var item in arr)
          {
              itemvalues = item.Split(new char[] { ‘=‘ }, StringSplitOptions.RemoveEmptyEntries);
              if (itemvalues == null || itemvalues.Length == 0) continue;
              nvcs.Add(itemvalues[0], itemvalues.Length <= 1 ? string.Empty : itemvalues[1]);
          }
          return nvcs;
        }

然后去看正则的文档 修改后版本

 public static NameValueCollection QueryString(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return null;
            }
            var m = Regex.Matches(path, @"(?<=\?|&)[\w\={},:‘‘""]*(?<=[^#])", RegexOptions.None);
            if (m.Count <= 0)
            {
                return null;
            }
            NameValueCollection nvcs = new NameValueCollection();
            string[] itemvalues = null;
            for (int i = 0; i < m.Count; i++)
            {
                itemvalues = m[i].Value.Split(new char[] { ‘=‘ }, StringSplitOptions.RemoveEmptyEntries);
                if (itemvalues == null || itemvalues.Length == 0) continue;
                nvcs.Add(itemvalues[0], itemvalues.Length <= 1 ? string.Empty : itemvalues[1]);
            }
            return nvcs;
        }
时间: 2024-08-28 00:56:16

正则表达式解析url参数的相关文章

jQuery 解析 url 参数

应用场景: 三毛:我现在拿到一个 url 地址(https://www.google.com/search?dcr=0&ei=5C&q=param),我现在要获取 location.search 后的参数,并组成一个对象,{dcr: '0', ej: '5C', q: 'param'},怎么处理? 五毛:呃,稍等,我去谷歌一下 谷歌结果: // 解析 url 参数 (function($) { var re = /([^&=]+)=?([^&]*)/g, decodeRE

解析URL参数

/** * 解析url参数 * @example ?id=12345&a=b * @return Object {id:12345,a:b} */ export function urlParse () { let url = window.location.search; let obj = {}; let reg = /[?&][^?&]+=[^?&]+/g; let arr = url.match(reg); // ['?id=12345','&a:b'] i

正则表达式获取URL参数

使用到的正则表达式: [^\?&]?参数名=[^&]+ document.location.getURLPara = function (name) { var reg = new RegExp("[^\?&]?" + encodeURI(name) + "=[^&]+"); var arr = this.search.match(reg); if (arr != null) { return decodeURI(arr[0].sub

nodejs解析url参数的三种方法

要解析的url:http://127.0.0.1:8090/?name=cpc&age=21 利用js字符串操作函数进行解析 const myserver = require("http"); const urlib = require("url"); var myfs = require("fs"); myserver.createServer(function (req,res){ console.log(req.url); res.

解析URL参数为JS对象

function parseQueryString(url){ var obj={}; var keyvalue=[]; var key="",value=""; var paraString=url.substring(url.indexOf("?")+1,url.length).split("&"); for(var i in paraString) { keyvalue=paraString[i].split(&

Android webView解析URL参数

2015年6月18日 13:56:21 星期四 又当爹又当娘啊............ 1 public void onPageFinished(WebView view, String url) 2 { 3 super.onPageFinished(view, url); 4 5 if (url.indexOf("?") > 0) { 6 String[] arrUrl = url.split("\\?"); 7 String[] arrArg = arrU

Scala正则和抽取器:解析方法参数

在<正则表达式基础知识>中概括了正则表达式的基础知识, 本文讲解如何使用正则表达式解析方法参数,从而可以根据 DAO 自动生成 Service. 在做 Java 项目时,常常要根据 DAO 生成 Service , 而 Service 有时是简单的调用 DAO 方法.比如根据 public CreativeDO findByCreativeId(Long creativeId)  生成如下代码: public CreativeDO findByCreativeId(Long creativeI

使用正则表达式对URL进行解析

对URL进行解析,一般用到的参数有: 1.协议 如http,https 2.域名或IP 3.端口号,如7001,8080 4.Web上下文 5.URI,请求资源地址 6.请求参数 一个URL示例: http://i0.sinaimg.cn:8080/blog/register.jsp?type=a&name=test1234 这里只对前5个参数进行匹配解析: //使用字符索引对URL进行解析 function parseURL(url){ //解析协议 var protocal = url.su

五种URL参数解析方法的性能比较

因为在最近项目中需要解析日志中的 URL 的参数,所以我对比了一下五种不同 的 URL 参数解析方法的性能.URL 参数解析方法: httpclient org.apache.http.client.utils.URLEncodedUtils URLEncodedUtils.parse(query, Charset.forName("UTF-8"));jettyUtil org.eclipse.jetty.util.UrlEncoded MultiMapvalues = new Mul