C # Regular matching, grouping, and replacing

Source: Internet
Author: User
Tags port number

ASP tutorials. NET C # matching, grouping, and replacing
There is a project cited WebService, because at the same time on several servers, in order to facilitate switching, I will be able to dynamically change its IP (only IP is different, not webservice different), so I just replace the server address part of it can be

2, to extract the desired data from the query string in order to restore the data to the Web page, similar to the tutorial viewstate features

private string Testrex (match m)
Group 0, which is all matches, followed by the parentheses in each
return m.groups tutorial [1] + "/" + m.groups[3];

protected void Regextest ()
Test regular replacement
string s = "";

Replace IP address plus port number for
Regex rx = new Regex (@ https?:/) (/[^/]+) (/.*)");

MatchEvaluator me = new MatchEvaluator (Testrex);
s = Rx.replace (S, me);//Method 1, the way of the delegate
s = Rx.replace (S, "$1/$3"); Method 2, how to replace a string
Not defining as a variable is to make it easier to demonstrate why I've split http://(http:/) and (/221.232.137 ...),
Like the top, if I split it up (https?:/ /), it will be written as Rx.replace (S, "$$3"), and obviously, the $ $88,
This is any case that you need to pay attention to when you represent a group number with $
On the contrary, the use of the above MatchEvaluator does not exist because of the group, not the $
Response.Write (s);
Response.Write ("<br/>");

Gets the date inside the query string
String depselectedsql = "1=1 and Reserve_date>=to_date" (' 2010-10-10 ', ' yyyy-mm-dd ') and Reserve_date<=to_date (' 2010-11-10 ', ' yyyy-mm-dd ');

Regex r = new Regex (@ "reserve_date.=to_date.") (DDDD-DD-DD) ", regexoptions.ignorecase);
MatchCollection mc = r.matches (Depselectedsql);
if (Mc.count < 2)
Response.Write ("Invalid query string!");
string sdate = Mc[0].groups[1].value;
string edate = Mc[1].groups[1].value;

Response.Write (sdate + "<br/>" + edate);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.