tags:

views:

129

answers:

2

I need to match the numbers inside a html tag. IE

Sometext<sometag><htmltag>123123</htmltag></sometag>

I need to create a regex that finds the number that is inside the html tag of my choice, in this case the <htmltag>

Thanks everyone :)

+1  A: 

If all there is between those two tags is the number, and absolutely no white space or anything, you can simply use this regex:

/<htmltag>([0-9]+)<\/htmltag>/

Or this if there might be whitespace:

/<htmltag>\s*([0-9]+)\s*<\/htmltag>/
yjerem
Thanks I see that what shows up in my question is not what I wrote down because it skipps to show my HTML tags > <
`\w` is not whitespace characters but word characters. `\s` is whitespace characters.
Gumbo
As Gumbo pointed out \w should be \s, but you might also want to change + to * in order to allow zero instances of space before and after the number. Also, \d is a nice shortcut for digits. Result: /<htmltag>\s*(\d+)\s*<\/htmltag>/
Jørn Schou-Rode
I use this code:Match m = Regex.Match(resultFromFetch, @"/<numberofcases>([0-9]+)<\/numberofcases>/");The code might be wrong ofc, but as it is not it returns false and does not return the number inside the tags.<resultset morerecords=\"0\"><result><numberofcases>1</numberofcases></result></resultset>"
Thanks Gumbo and Jørn, I was in an unnecessary hurry... Andreas, what language is that? It might not require the slashes at the beginning and end, so try it without them. Which also means you won't need the backslash to escape the slash in </numberofcases>.
yjerem
It is C# that does a fetchxml query and the result is the xml u see in my post above. I need to get a hold of the innerxml. I could ofc do it easily with regular code, but I want to do it with a regex.Thaks for all your help guys :)
+3  A: 

No, you don't need to "match", you need to extract an HTML node. Use an HTML parser. An HTML parser is simpler to use, more robust against changes, and easier to extend (e.g. grabbing more parts of the same document). A regular expression, on the other hand, is just the wrong tool, because HTML is not a regular language.

Svante
Ok, I will scrap my little regex exploration and return to familiar grounds then. I can't get it work anywayThanks everyone.