✅ Regex to capture all attributes of html tag but it capture the last only.
Hi,
i have this code (see the picture)
i don't understand why i have the last attribute only.
i need have all.
please, help me.
code to use:
MatchCollection attrmatches = new Regex(@"<[a-zA-Z.]+(?:\s+(\w+)\s*=\s*""[^""]*"")*\s*>", RegexOptions.Compiled).Matches(line);
thx16 Replies
looks like you're reading a XML file why not just use a library to read xml
https://stackoverflow.com/a/1732454
General note on HTML and trying to use regex to parse it. Unless you want something really simple it won't be happening.
Stack Overflow
RegEx match open tags except XHTML self-contained tags
I need to match all of these opening tags:
<p>
<a href="foo">
But not self-closing tags:
<br />
<hr class="foo" />
I came up with this and wanted to make
What exactly are you trying to do?
Do you have examples on what should/shouldn't be matched?
Give more detailed information. For example your input text and the expected output. It should be easy to solve.
AngleSharp
is a very good html parser library for C#
would make this trivial to solvehi, thanx but i don't want use a library (i ask it ?).
my line to read is in SyncRoot var (read the picture).
i try to do that u see exactly . use a regex to capture all attributes in <tag >
@talk is cheap, show me the code
but, i need to identify the tag too to get his index and length in my line.
if i use your regex, when i have a lot of tag in same line or text + a tag, i can't know who is the good tag (position in text).
eg: blablabla <tag f="tt"> <tag j="klkll"> blabla
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
i undersand but why the online regex can analyse the same line correctly ?
https://regex101.com/
choose .net
Pattern
<[\w.]+(?:\s+(\w+)\s*=\s*""[^""]*"")*\s*>
text : <SimBase.Document Type="MissionFile" version="1,0">
it can capture all attributes.
for me, this is not because the html is irregular and regex is regular,
regex can parse my text but if i use in a real code, it fail.
same with http://regexstorm.net/tester
Pattern : <[\w.]+(?:\s+(\w+)\s*=\s*\"[^\"]*\")*\s*/?>
regex101
regex101: build, test, and debug regex
Regular expression tester with syntax highlighting, explanation, cheat sheet for PHP/PCRE, Python, GO, JavaScript, Java, C#/.NET, Rust.
.NET Regex Tester - Regex Storm
Online .NET regular expression tester with real-time highlighting and detailed results output.
honestly, you shoudl not try to parse html using regex. it is not valid to do so. using a library is a better means of accomplishing your goal.
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
@talk is cheap, show me the codethx it work, i see my error comparing with my code
You can close this question by typing
/close
and pressing enter.hip hip hip oura for @talk is cheap, show me the code