如何获取总元素标签?

huangapple go评论102阅读模式
英文:

How to get total element tag?

问题

以下是您要翻译的部分:

  1. 我有一个包含一些标签的字符串列表,我想计算每个标签中的元素总数。
  2. <!-- language: lang-html -->
  3. <L1 //2 element
  4. <L1 //1 element
  5. <H1 content>
  6. >
  7. <L1 //3 element
  8. <H2 content>
  9. <P content>
  10. <L1 //1 element
  11. <H3 content>
  12. >
  13. >
  14. >
  15. <!-- end snippet -->
  16. 这是我的C#代码片段
  17. var str = "&lt;L1\r\n &lt;L1\r\n &lt;H1 content&gt; \r\n &gt; \r\n &lt;L1\r\n &lt;H2 content&gt; \r\n &lt;P content&gt;\r\n &lt;L1\r\n &lt;H3 content&gt;\r\n &gt;\r\n &gt;\r\n&gt;";
  18. var list = str.Split(new string[] { "\r\n" }, StringSplitOptions.None);
  19. var array_num = new List<string>();
  20. int startpos = 0, endpos = 0, total = 0, newstartpos = 0;
  21. bool newtag = false;
  22. for (int i = 0; i < list.Length; i++)
  23. {
  24. if (list[i].Trim() == "<L1")
  25. {
  26. startpos = i;
  27. for (int Lindex = i + 1; Lindex < list.Length ; Lindex++)
  28. {
  29. var item = list[Lindex].Trim().ToString();
  30. if (list[Lindex].Trim().StartsWith("<L1") && list[Lindex].Trim().EndsWith(">"))
  31. {
  32. total += 1;
  33. }
  34. if (list[Lindex].Trim() == "<L1")
  35. {
  36. total += 2;
  37. newstartpos = Lindex;
  38. newtag = true;
  39. }
  40. if (list[Lindex].Trim() == ">" && newstartpos != 0)
  41. {
  42. total -= 1;
  43. endpos = Lindex;
  44. newtag = false;
  45. }
  46. if (list[Lindex].Trim().StartsWith("<") && list[Lindex].Trim().EndsWith(">") && !newtag)
  47. {
  48. total += 1;
  49. }
  50. if (list[Lindex].Trim() == ">" && newstartpos == 0)
  51. {
  52. endpos = Lindex;
  53. break;
  54. }
  55. }
  56. array_num.Add("start: " + startpos + " end: " + endpos + " count: " + total);
  57. startpos = 0;
  58. endpos = 0;
  59. total = 0;
  60. newstartpos = 0;
  61. newtag = false;
  62. }
  63. }
  64. 但当我运行它并获得不符合预期的结果时,结果```array_num```包含内容。结果是正确的应该是
  65. <!-- language: lang-html -->
  66. start:0 end: 11 count: 2 //正确
  67. start:1 end: 3 count: 1 //正确
  68. start:4 end: 11 count: 1 //不正确应该是 4 10 3
  69. start:7 end: 9 count: 1 //正确
  70. <!-- end snippet -->
  71. 但我不确定我的代码在其他示例中是否稳定工作。如果对此有任何想法,或需要进行调整,请告诉我以进行更改。
  72. <details>
  73. <summary>英文:</summary>
  74. I have the list string with some tags is defined, I want to count how many total element in each tag.
  75. &lt;!-- language: lang-html --&gt;
  76. &lt;L1 //2 element
  77. &lt;L1 //1 element
  78. &lt;H1 content&gt;
  79. &gt;
  80. &lt;L1 //3 element
  81. &lt;H2 content&gt;
  82. &lt;P content&gt;
  83. &lt;L1 //1 element
  84. &lt;H3 content&gt;
  85. &gt;
  86. &gt;
  87. &gt;
  88. &lt;!-- end snippet --&gt;
  89. This is my snipped code C#
  90. var str = &quot;&lt;L1\r\n &lt;L1\r\n &lt;H1 content&gt; \r\n &gt; \r\n &lt;L1\r\n &lt;H2 content&gt; \r\n &lt;P content&gt;\r\n &lt;L1\r\n &lt;H3 content&gt;\r\n &gt;\r\n &gt;\r\n&gt;&quot;;
  91. var list = str.Split(new string[] { &quot;\r\n&quot; }, StringSplitOptions.None);
  92. var array_num = new List&lt;string&gt;();
  93. int startpos = 0, endpos = 0, total = 0, newstartpos = 0;
  94. bool newtag = false;
  95. for (int i = 0; i &lt; list.Length; i++)
  96. {
  97. if (list[i].Trim() == &quot;&lt;L1&quot;)
  98. {
  99. startpos = i;
  100. for (int Lindex = i + 1; Lindex &lt; list.Length ; Lindex++)
  101. {
  102. var item = list[Lindex].Trim().ToString();
  103. if (list[Lindex].Trim().StartsWith(&quot;&lt;L1&quot;) &amp;&amp; list[Lindex].Trim().EndsWith(&quot;&gt;&quot;))
  104. {
  105. total += 1;
  106. }
  107. if (list[Lindex].Trim() == &quot;&lt;L1&quot;)
  108. {
  109. total += 2;
  110. newstartpos = Lindex;
  111. newtag = true;
  112. }
  113. if (list[Lindex].Trim() == &quot;&gt;&quot; &amp;&amp; newstartpos != 0)
  114. {
  115. total -= 1;
  116. endpos = Lindex;
  117. newtag = false;
  118. }
  119. if (list[Lindex].Trim().StartsWith(&quot;&lt;&quot;) &amp;&amp; list[Lindex].Trim().EndsWith(&quot;&gt;&quot;) &amp;&amp; !newtag)
  120. {
  121. total += 1;
  122. }
  123. if (list[Lindex].Trim() == &quot;&gt;&quot; &amp;&amp; newstartpos == 0)
  124. {
  125. endpos = Lindex;
  126. break;
  127. }
  128. }
  129. array_num.Add(&quot;start: &quot; + startpos + &quot; end: &quot; + endpos + &quot; count: &quot; + total);
  130. startpos = 0;
  131. endpos = 0;
  132. total = 0;
  133. newstartpos = 0;
  134. newtag = false;
  135. }
  136. }
  137. But when I run it and get the result not expected, the result ```array_num``` get content. The result is correct should be
  138. &lt;!-- language: lang-html --&gt;
  139. start:0 end: 11 count: 2 //correct
  140. start:1 end: 3 count: 1 //correct
  141. start:4 end: 11 count: 1 //incorrect should be 4 10 3
  142. start:7 end: 9 count: 1 //correct
  143. &lt;!-- end snippet --&gt;
  144. But i&#39;m not sure my code is working stable with other example, If you have any idea for this, or adjust something, kindly let me know for changed something.
  145. </details>
  146. # 答案1
  147. **得分**: 1
  148. 以下是翻译好的部分:
  149. ```csharp
  150. 一个堆栈结构是最好的选择,因为它反映了正在解析的内容的结构。
  151. 以下是一种解决方案。使用一个堆栈和一个TagCounter类。TagCounter类跟踪标签的子项数量,它是否是一个L1标签以及它在字符串中的索引,以便它们可以在最后正确排序:
  152. internal class TagCounter
  153. {
  154. public TagCounter(bool isL1Tag, int index)
  155. {
  156. ChildCount = 0;
  157. IsL1Tag = isL1Tag;
  158. Index = index;
  159. }
  160. public int ChildCount { get; set; }
  161. public bool IsL1Tag { get; private set; }
  162. public int Index { get; set; }
  163. }
  164. 计算的代码片段:
  165. var str = "&lt;L1\r\n &lt;L1\r\n &lt;H1 content&gt; \r\n &gt; \r\n &lt;L1\r\n &lt;H2 content&gt; \r\n &lt;P content&gt;\r\n &lt;L1\r\n &lt;H3 content&gt;\r\n &gt;\r\n &gt;\r\n&gt;";
  166. var openTags = new Stack<TagCounter>();
  167. var parsedLTags = new List<TagCounter>();
  168. var shortenedString = str.Replace("\r\n", "");
  169. TagCounter? currentTag = null;
  170. var stringLength = shortenedString.Length;
  171. for (var i = 0; i < stringLength; i++)
  172. {
  173. var nextChar = shortenedString[i];
  174. if (nextChar == '<')
  175. {
  176. if (currentTag != null)
  177. {
  178. currentTag.ChildCount++;
  179. }
  180. var isL1Tag = shortenedString.Substring(i + 1, 2).Equals("L1");
  181. if (currentTag != null)
  182. {
  183. openTags.Push(currentTag);
  184. }
  185. currentTag = new TagCounter(isL1Tag, i);
  186. }
  187. else if (nextChar == '>')
  188. {
  189. if (currentTag.IsL1Tag)
  190. {
  191. parsedLTags.Add(currentTag);
  192. }
  193. if (openTags.Any())
  194. {
  195. currentTag = openTags.Pop();
  196. }
  197. else
  198. {
  199. currentTag = null;
  200. }
  201. }
  202. }
  203. var result = parsedLTags.OrderBy(x=>x.Index).Select(x=>x.ChildCount).ToList();

希望对您有所帮助!如果您有任何其他问题或需要进一步帮助,请随时告诉我。

英文:

A stack structure would be best to use as it reflects the structure of content being parsed.

Here is one solution. Use a stack and a TagCounter class. The TagCounter class tracks the number of children a tag has, whether it is an L1 tag, and its index in the string so they can be put into the correct order at the end:

  1. internal class TagCounter
  2. {
  3. public TagCounter(bool isL1Tag, int index)
  4. {
  5. ChildCount = 0;
  6. IsL1Tag = isL1Tag;
  7. Index = index;
  8. }
  9. public int ChildCount { get; set; }
  10. public bool IsL1Tag { get; private set; }
  11. public int Index { get; set; }
  12. }

snippet to compute it:

  1. var str = &quot;&lt;L1\r\n &lt;L1\r\n &lt;H1 content&gt; \r\n &gt; \r\n &lt;L1\r\n &lt;H2 content&gt; \r\n &lt;P content&gt;\r\n &lt;L1\r\n &lt;H3 content&gt;\r\n &gt;\r\n &gt;\r\n&gt;&quot;;
  2. var openTags = new Stack&lt;TagCounter&gt;();
  3. var parsedLTags = new List&lt;TagCounter&gt;();
  4. var shortenedString = str.Replace(&quot;\r\n&quot;, &quot;&quot;);
  5. TagCounter? currentTag = null;
  6. var stringLength = shortenedString.Length;
  7. for (var i = 0;i &lt; stringLength; i++)
  8. {
  9. var nextChar = shortenedString[i];
  10. if (nextChar == &#39;&lt;&#39;)
  11. {
  12. if (currentTag != null)
  13. {
  14. currentTag.ChildCount++;
  15. }
  16. var isL1Tag = shortenedString.Substring(i + 1, 2).Equals(&quot;L1&quot;);
  17. if (currentTag != null)
  18. {
  19. openTags.Push(currentTag);
  20. }
  21. currentTag = new TagCounter(isL1Tag, i);
  22. }
  23. else if (nextChar == &#39;&gt;&#39;)
  24. {
  25. if (currentTag.IsL1Tag)
  26. {
  27. parsedLTags.Add(currentTag);
  28. }
  29. if (openTags.Any())
  30. {
  31. currentTag = openTags.Pop();
  32. }
  33. else
  34. {
  35. currentTag = null;
  36. }
  37. }
  38. }
  39. var result = parsedLTags.OrderBy(x=&gt;x.Index).Select(x=&gt;x.ChildCount).ToList();

huangapple
  • 本文由 发表于 2023年7月6日 15:28:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76626441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定