can anyone think of an elegant way of getting the ...
# getting-started
d
can anyone think of an elegant way of getting the tag-name of a html-tag? currently i have
"<my-tag>...</my-tag>".removePrefix("<").takeWhile { it != '>' }
s
that’ll get you all the attributes in the tag too
plus it’ll get you weird results on doctype statements and cdata sections if you ever run into those
d
that’s true, but in my case it should always be on this form (2+ words joined by dashes, no attributes)
m
you could try with a regex
Copy code
val re = Regex("<(\\w+)>") 
re.find("<my-tag>...</my-tag>")?.groupValues?.get(1)
obviously this works on very simple strings containing one tag, a regex can’t parse a full xml document
s
definitely an important caveat. XML/HTML is not a regular language and cannot be parsed by regular expressions
1
👆 1
m
Oh, Shawn is right… so for this simple case change the regex to
<([-\w]+)
d
thanks @Matteo Mirk, that’ll do nicely for my case
👍 1
m
a true classic 👌