Skip to content

Instantly share code, notes, and snippets.

@JerryBian
Created July 30, 2012 06:32
Show Gist options
  • Save JerryBian/3205340 to your computer and use it in GitHub Desktop.
Save JerryBian/3205340 to your computer and use it in GitHub Desktop.
C# code,Regular Expression,obatin special url from html string
//gdfgfdg<a href="/excel/dsfsf-).xls">/excel/dsfsf-).xls</a>dsfdsfdsf....
internal class Program
{
public static string source = @"C:\Users\JBian\Documents\visual studio 2010\Projects\ConsoleApplication23\ConsoleApplication23\Files\source.txt";
public static string result = @"C:\Users\JBian\Documents\visual studio 2010\Projects\ConsoleApplication23\ConsoleApplication23\Files\result.txt";
private static void Main(string[] args)
{
StreamReader sd = new StreamReader(source);
string pattern1 = "(<a href=\"\\/excel\\/[\\w|\\-|\\(|\\)]+\\.(xls|html)\">[\\w|\\-|\\(|\\)]+\\.(xls|html)</a>)";
Regex reg1 = new Regex(pattern1, RegexOptions.IgnoreCase);
var qq = reg1.Matches(sd.ReadToEnd());
string str = string.Empty;
for (int i = 0; i < qq.Count; i++)
{
string cap = qq[i].Value;
string pattern2 = "[\\w|\\-|\\(|\\)]+\\.(xls|html)";
Regex reg2 = new Regex(pattern2, RegexOptions.IgnoreCase);
if (reg2.Match(cap).Success)
{
string result = reg2.Match(cap).Value;
Console.WriteLine(result);
str += result + Environment.NewLine;
}
}
OutputResult(str);
Console.ReadKey();
}
private static void OutputResult(string str)
{
using (StreamWriter sw = new StreamWriter(result))
{
sw.WriteLine(str + Environment.NewLine);
sw.Flush();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment