简体   繁体   中英

How do I allow whitespace with Regex.Replace

I have created web service method in asp.net c#, and I am using Regex.Replace method beacause of some invalid characters are returning in result, Now how do I allow white space in that method,because in Regex.Replace method, white space is eliminated. see my code:

public string list(long empid, string name, string pwd, long id)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(link);
    request.Connection = Regex.Replace((empid, name, pwd, id), "[^\"a-zA-Z0-9_?<>+=./:-]", "");
    string connection = request.Connection;
    return connection;
}

if I use space like in regex.Replace like this:

request.Connection = Regex.Replace((empid, name, pwd, id), "[^\"a-zA-Z0-9_?<>+=./:- ]", "");

I am getting error:

System.ArgumentException: parsing &quot;[^&quot;a-zA-Z0-9_?&lt;&gt;+=./:- ]&quot; - [x-y] range in reverse order.

You can simply add that character to your class.

[^\"a-zA-Z0-9_?<>+=./: -]

If I use space like this: [^\\"a-zA-Z0-9_?<>+=./:- ] I am getting an error....

Within a character class [ ] you can place a hyphen as the first or last character in the range. If you place the hyphen anywhere else you need to escape it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM