标签:
解析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; }
标签:
原文地址:http://www.cnblogs.com/LQBlog/p/5508529.html