Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

Just Browsing Browsing

[1] html - How to create even cell spacing within a

548k questions

547k answers

4 comments

86.3k users

...