用于查询字符串值.NET正则表达式 [英] .NET Regular expression for querystring value

查看:140
本文介绍了用于查询字符串值.NET正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要去掉的话;从Url.PathAndQuery&放大器ID = someValue中。凡someValue中可能是一个int或字符串。它可以或可以不被随后的另一个符号



因此,它可以是



  SomePage.aspx页面猫= 22安培;?ID = SomeId&放大器;参数2 = 4 

  SomePage.aspx页面猫= TECT&放大器;?ID = 450 

我想会留下

  SomePage.aspx页面?猫= 22安培;参数2 = 4 

  SomePage.aspx页面?猫= TECT 


解决方案

<击>



刚准备把我的头顶部...

 字符串的URL =?SomePage.aspx页面猫= 22和ID = SomeId&放大器;参数2 = 4; 
正则表达式的regex =新的正则表达式(([?\&安培])ID =?^ \&安培] +);
URL = regex.replace(URL,\1);
System.Diagnostics.Debug.WriteLine(URL =+网址);





更新2010-03-05下午11时12 PST



我一直到实际测试我的代码中的注释羞愧。什么是你,我的QA部门? 。下面是使用MSTest的工作示例

 正则表达式的regex =新的正则表达式(@([\&放大器;?]) ID = [^ \&安培] + [\&安培])?; 

[TestMethod的]
公共无效RegexReplacesParameterInMiddle()
{
字符串URL =SomePage.aspx页面猫= 22安培;?ID = SomeId&放大器;参数2 = 4;
URL = regex.Replace(URL,$ 1);
Assert.AreEqual(猫SomePage.aspx页面= 22&放大器;参数2 = 4,URL);
}

[TestMethod的]
公共无效RegexReplacesParameterInFront()
{
字符串URL =SomePage.aspx页面ID = SomeId&放大器;猫= 22安培;参数2 = 4;
URL = regex.Replace(URL,$ 1);
Assert.AreEqual(猫SomePage.aspx页面= 22&放大器;参数2 = 4,URL);
}

[TestMethod的]
公共无效RegexReplacesParameterAtEnd()
{
字符串URL =SomePage.aspx页面猫= 22安培;参数2 = 4和; ID = SomeId;
URL = regex.Replace(URL,$ 1);
Assert.AreEqual(SomePage.aspx页面猫= 22&放大器; param2的= 4和,网址);
}

[TestMethod的]
公共无效RegexReplacesSoleParameter()
{
字符串URL =SomePage.aspx页面ID = SomeId?
URL = regex.Replace(URL,$ 1);
Assert.AreEqual(SomePage.aspx页面?,网址);
}

公共无效RegexIgnoresMissingParameter()
{
字符串URL =SomePage.aspx页面富=酒吧和放大器; blet =猴子;
URL = regex.Replace(URL,$ 1);
Assert.AreEqual(?SomePage.aspx页面富=酒吧和放大器; blet =猴子,URL);
}



正则表达式,解释说:

 查找一个?或与&字符(和将其存储为一个反向引用)
,接着为ID =
,接着通过一个或多个非 - 与&字符。
可选紧接着又与&



然后替换表达反向引用,这样你就不会失去你的初始?/&放;.



注意 - 因为你可以从测试看,这发出一个尾随?或放大器;当替换参数是分别在只有一个或最后一个,。您可以使用字符串的方法来摆脱这一点,但如果有人知道如何只用正则表达式来保持他们的结果,这将是极好的看到。


I need to strip out any "&id=SomeValue" from a Url.PathAndQuery. Where SomeValue could be an int or a string. And it may or may not be followed by another ampersand.

So it could be

somepage.aspx?cat=22&id=SomeId&param2=4

or

somepage.aspx?cat=tect&id=450

I want to be left with

somepage.aspx?cat=22&param2=4

or

somepage.aspx?cat=tect

解决方案

Just going off the top of my head...

string url = "somepage.aspx?cat=22&id=SomeId&param2=4";
Regex regex = new Regex("([\?\&])id=[^\?\&]+");
url = regex.replace(url, "\1");
System.Diagnostics.Debug.WriteLine("url = " + url);

Update 2010-03-05 11:12 PM PST

I've been shamed by a comment into actually testing my code. What are you, my QA department? Here's a working example using MSTest.

    Regex regex = new Regex(@"([\?\&])id=[^\&]+[\&]?");

    [TestMethod]
    public void RegexReplacesParameterInMiddle()
    {
        string url = "somepage.aspx?cat=22&id=SomeId&param2=4";
        url = regex.Replace(url, "$1");
        Assert.AreEqual("somepage.aspx?cat=22&param2=4",url);
    }

    [TestMethod]
    public void RegexReplacesParameterInFront()
    {
        string url = "somepage.aspx?id=SomeId&cat=22&param2=4";
        url = regex.Replace(url, "$1");
        Assert.AreEqual("somepage.aspx?cat=22&param2=4", url);
    }

    [TestMethod]
    public void RegexReplacesParameterAtEnd()
    {
        string url = "somepage.aspx?cat=22&param2=4&id=SomeId";
        url = regex.Replace(url, "$1");
        Assert.AreEqual("somepage.aspx?cat=22&param2=4&", url);
    }

    [TestMethod]
    public void RegexReplacesSoleParameter()
    {
        string url = "somepage.aspx?id=SomeId";
        url = regex.Replace(url, "$1");
        Assert.AreEqual("somepage.aspx?", url);
    }

    public void RegexIgnoresMissingParameter()
    {
        string url = "somepage.aspx?foo=bar&blet=monkey";
        url = regex.Replace(url, "$1");
        Assert.AreEqual("somepage.aspx?foo=bar&blet=monkey", url);
    }

The regex, interpreted, says:

Look for a "?" or an "&" character (and store it as a backreference)
followed by "id="
followed by one or more non-"&" characters.
optionally followed by another "&"

Then replace that expression with the backreference, so you don't lose your initial ?/&.

note -- as you can see from the tests, this emits a trailing ? or & when the replaced parameter is the only one or the last one, respectively. You could use string methods to get rid of that, though if somebody knows how to keep them out of the result using only regular expressions it would be excellent to see.

这篇关于用于查询字符串值.NET正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆