Skip to content Skip to sidebar Skip to footer

Regex To Extract Text From Inside An Html Tag

I know this has been asked at least a thousand times but I can't find a proper regex that will match a name in this string here:
Donald<

Solution 1:

Josh's pattern will work if you only make use of the numbered group, not the whole match. If you have to use the whole match, use something like (?<=>)(\w+?)(?=<)

Either way, regex is widely known to not be good for parsing HTML.

Explanation: (?<=) is used to check if something appears before the current item. \w+? will match any "word"-character, one or more times, but stop whenever the rest of the pattern matches something, for this situation the ? could have been left out. (?=) is used to check if something appears after the current item.

Solution 2:

Try this

It should capture anything that is a letter / number

>([\w]+)<

Also I'm not exactly sure what your project limitations are, but it would be much easier to do something like this

$('#topbarUserName').text();

in jQuery instead of using a regex.

Solution 3:

>([a-zA-Z]+) should do the Trick. Remember to get the grouping right.

Solution 4:

Why not doing it with plain old basic string-functions?

a.w = FindString(HTMLstring.s, "topbarUserName") + 16 ; 2 for"> and topbar...
If a > 0
b.w = FindString(HTMLstring, "<", a)
If b > 0
c.w = b - a
Donald.s = Mid(HTMLstring,a, c)
EndIf
EndIf
Debug Donald

Post a Comment for "Regex To Extract Text From Inside An Html Tag"