Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

asp.net - How to get the string from html file?

How should I search for and get a string from html files using c# in asp.net? This is the code:

 private string getHtml(string key)
 {
     StreamReader f = new StreamReader("path");
     string htmlTag = key;
     string str = f.ReadToEnd().ToString();
     Match m = Regex.Match(str, "<" + htmlTag + ">" + "(.*)" + "</" +   
                                htmlTag + ">", RegexOptions.Singleline);
     Console.WriteLine(m.Groups[0]);             
     return str;
 }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your RegEx, try changing this:

"(.*)"

to this:

"([^<]*)"

So, instead of matching ANY character, you match any characters up to (but not including) the next less-than symbol.

You might also want to change this:

"</" + htmlTag + ">"

to this

"</ ?" + htmlTag + ">"

To allow for a space after the slash (you can ignore this second suggestion if you have full control over the HTML documents and know exactly how they were coded)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...