C # Regular expression gets the specified string information

Source: Internet
Author: User
Tags regular expression

Example 1

The code is as follows Copy Code

private void Button2_Click (object sender, EventArgs e)
{
//Create a request for the URL.
WebRequest request = WebRequest.Create ("http://www.111cn.net/");
//If required by the server, set the credentials.
Request. Credentials = CredentialCache.DefaultCredentials;
//Get the response.
HttpWebResponse response = (HttpWebResponse) request. GetResponse ();
//Display the status.
MessageBox.Show (response. Statusdescription);
Console.WriteLine (Response. Statusdescription); The
//get the stream containing content returned by the server.
Stream DataStream = response. GetResponseStream (); The
//Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (DataStream, Encoding.default);
//Read the content.
String responsefromserver = reader. ReadToEnd ();
//intercepting data
int i = responsefromserver.indexof ("Beijing");
String databid = responsefromserver.substring (i);

Display the content.
MessageBox.Show (Databid);
Console.WriteLine (Responsefromserver);
Cleanup the streams and the response.
Reader. Close ();
Datastream.close ();
Response. Close ();
}

C # Obtains a text in the middle of a specified start and end string with a regular expression

The code is as follows Copy Code

<summary>
Gets the median value of the start and end strings in a string
</summary>
<param name= "str" ></param>
<param name= "S" > Start </param>
<param name= "E" > End </param>
<returns></returns>
public static string GetValue (String str, string s, String e)
{
Regex RG = new Regex (? <= ("+ S +")) [. \s\s]*? = ("+ E +")) ", Regexoptions.multiline | Regexoptions.singleline);
Return RG. Match (str). Value;
}


<summary>
Intercepting string too much + ...
</summary>
<param name= "InputString" > String </param>
<param name= "len" > Length </param>
<returns>string</returns>
public static string Cutstring (string inputstring, int len)
{
ASCIIEncoding ASCII = new ASCIIEncoding ();
int templen = 0;
String tempstring = "";
Byte[] s = ASCII. GetBytes (inputstring);
for (int i = 0; i < s.length; i++)
{
if ((int) s[i] = = 63)
{
Templen + 2;
}
Else
{
Templen + 1;
} try
{
TempString + + inputstring.substring (i, 1);
}
Catch
{
Break
} if (Templen > Len)
Break
}
If it's truncated, add half an ellipsis.
byte[] MyByte = System.Text.Encoding.Default.GetBytes (inputstring);
if (MyByte. Length > Len)
TempString + = "..."; return tempstring;
}

Call method

  code is as follows copy code

String Get string = "01-blue";
String Get after = Get string. Substring (0, get string. IndexOf ("-")); The
gets the data that is: blue string get after = getting string. Substring (Get string. IndexOf ("-"), get string. Length-get string. IndexOf ("-"));

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: info-contact@alibabacloud.com 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.