简体   繁体   中英

C# Extract string from wall of text

I have a wall of html code from a source and I need to extract '1929485' from the source

<input type="hidden" name="key" value="1929485" />

How would I do this? found this online:

var match = Regex.Match(source, @"class="""" onclick=""NewWindow\('([^']*)',\s*'([^']*)',.*");

Unsure what this all means and does?

Thanks.

First, use

pos = htmlstring.IndexOf("1929485")

to find the index where the substring is. Make sure there aren't any other instances or the first you get might not be the one you need.

Then, expand to the start and to the end until you reach your sweet spot, like this:

startpos = htmlstring.LastIndexOf("<input", pos);
endpos = htmlstring.IndexOf("/>", pos) + 2;

Then extract the whole thing:

htmltag = htmlstring.Substring(startpos, endpos - startpos);

I might be off by one character, just experiment a bit to fit your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM