如何从Java中的HTML字符串中删除<h1>标签

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

How to remove <h1> tags from a html string java

问题

我有一个字符串

String mystring = "<html><head><meta name=\"viewport\" content=\"width=device-width\"></head><body><h1><a href=\"q://d?EN&amp;hello\">hello</a></h1>...";

我想要移除 mystring 中的

&lt;h1&gt;&lt;a href=&quot;q://d?EN&amp;hello&quot;&gt;hello&lt;/a&gt;&lt;/h1&gt;

我尝试了:https://stackoverflow.com/a/32005143/1083093... 但这不起作用,因为我只想删除 h1 标签以及其之间的内容。

英文:

I have a string

String mystring = &quot;&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width&quot;&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;&lt;a href=&quot;q://d?EN&amp;hello&quot;&gt;hello&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;
&lt;h3&gt;Alternative forms&lt;/h3&gt;
&lt;ul&gt;&lt;li&gt; &lt;a href=&quot;q://d?&amp;hallo&quot;&gt;hallo&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; &lt;a href=&quot;q://d?&amp;hilloa&quot;&gt;hilloa&lt;/a&gt; (obsolete)&lt;/li&gt;
&lt;li&gt; &lt;a href=&quot;q://d?&amp;hullo&quot;&gt;hullo&lt;/a&gt; (UK)&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Etymology&lt;/h3&gt;
&lt;a href=&quot;q://d?&amp;Hello&quot;&gt;hello&lt;/a&gt; (first attested in 1833), from &lt;a href=&quot;q://d?&amp;holla&quot;&gt;holla&lt;/a&gt;, &lt;a href=&quot;q://d?&amp;hollo&quot;&gt;hollo&lt;/a&gt; (attested 1588). This variant of &lt;a href=&quot;q://d?&amp;hallo&quot;&gt;hallo&lt;/a&gt; is often credited to Thomas Edison as a coinage for telephone use, but its appearance in print predates the invention of the telephone by several decades.Ultimately from a variant of Old English &lt;em&gt;&lt;a href=&quot;q://d?&amp;eala&quot;&gt;&amp;#x113;al&amp;#x101;&lt;/a&gt;&lt;/em&gt;, such as &lt;em&gt;&lt;a href=&quot;q://d?&amp;hela&quot;&gt;h&amp;#x113;l&amp;#x101;&lt;/a&gt;&lt;/em&gt;, which was used colloquially in that time similarly to how &amp;quot;hey&amp;quot; or &amp;quot;hi&amp;quot; is used nowadays. Thus, equivalent to a compound of &lt;em&gt;&lt;a href=&quot;q://d?&amp;hey&quot;&gt;hey&lt;/a&gt;&lt;/em&gt; and &lt;em&gt;&lt;a href=&quot;q://d?&amp;lo&quot;&gt;lo&lt;/a&gt;&lt;/em&gt;.Possibly influenced by the lang:goh and lang:osx verb &lt;a href=&quot;q://d?&amp;halon&quot;&gt;halon&lt;/a&gt;, &lt;a href=&quot;q://d?&amp;holon&quot;&gt;holon&lt;/a&gt; (&amp;quot;to bring something, to gather&amp;quot;), akin to English &lt;a href=&quot;q://d?&amp;hale&quot;&gt;hale&lt;/a&gt; or &lt;a href=&quot;q://d?&amp;hail&quot;&gt;hail&lt;/a&gt;. More at {{l/en|hallo}}.
&lt;h3&gt;Pronunciation&lt;/h3&gt;
&lt;ul&gt;&lt;li&gt; {{a|UK}} IPA: /h&amp;#x259;&amp;#x2c8;l&amp;#x259;&amp;#x28a;&amp;#x32f;/, /h&amp;#x25b;&amp;#x2c8;l&amp;#x259;&amp;#x28a;&amp;#x32f;/&lt;/li&gt;
&lt;li&gt; {{a|US}} {{enPR|h&amp;#x115;-l&amp;#x14d;&amp;#x27;|h&amp;#x259;-l&amp;#x14d;&amp;#x27;}}, IPA: /h&amp;#x25b;&amp;#x2c8;lo&amp;#x28a;&amp;#x32f;/, /h&amp;#x259;&amp;#x2c8;lo&amp;#x28a;&amp;#x32f;/&lt;/li&gt;
&lt;li&gt; {{audio|En-uk-hello.ogg|Audio (UK)|lang=en}}&lt;/li&gt;
&lt;/ul&gt;
{|class=&amp;quot;wikitable&amp;quot;! Sense! UK! US|-|{{sense|greeting}}|{{audio|en-uk-hello-1.ogg|Audio (UK)|lang=en}}|{{audio|en-us-hello.ogg|Audio (US)|lang=en}}|-|{{sense|telephone greeting}}|{{audio|en-uk-hello-2.ogg|Audio (UK)|lang=en}}|{{audio|en-us-hello-2.ogg|Audio (US)|lang=en}}|-|{{sense|call for response}}|{{audio|en-uk-hello-3.ogg|Audio (UK)|lang=en}}|{{audio|en-us-hello-3.ogg|Audio (US)|lang=en}}|-|{{sense|sarcastic implication}}|{{audio|en-uk-hello-4.ogg|Audio (UK)|lang=en}}|{{audio|en-us-hello-4.ogg|Audio (US)|lang=en}}|-|{{sense|expressing puzzlement}}|{{audio|en-uk-hello-5.ogg|Audio (UK)|lang=en}}||}
&lt;ul&gt;&lt;li&gt; {{rhymes|&amp;#x259;&amp;#x28a;|lang=en}}&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Interjection&lt;/h3&gt;
{en-interj}
&lt;ol&gt;&lt;li&gt; {{non-gloss definition|A &lt;a href=&quot;q://d?&amp;greeting&quot;&gt;greeting&lt;/a&gt; (&lt;a href=&quot;q://d?&amp;salutation&quot;&gt;salutation&lt;/a&gt;) said when &lt;a href=&quot;q://d?&amp;meet&quot;&gt;meet&lt;/a&gt;ing someone or &lt;a href=&quot;q://d?&amp;acknowledge&quot;&gt;acknowledging&lt;/a&gt; someone&amp;#x2019;s &lt;a href=&quot;q://d?&amp;arrival&quot;&gt;arrival&lt;/a&gt; or &lt;a href=&quot;q://d?&amp;presence&quot;&gt;presence&lt;/a&gt;.}}&lt;/li&gt;
&lt;ul&gt;&lt;li&gt; {{usex|&lt;b&gt;Hello,&lt;/b&gt; everyone.|lang=en}}&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt; {{non-gloss definition|A greeting used when &lt;a href=&quot;q://d?&amp;answer&quot;&gt;answer&lt;/a&gt;ing the &lt;a href=&quot;q://d?&amp;telephone&quot;&gt;telephone&lt;/a&gt;.}}&lt;/li&gt;
&lt;ul&gt;&lt;li&gt; {{usex|&lt;b&gt;Hello&lt;/b&gt;? How may I help you?|lang=en}}&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt; {{non-gloss definition|A call for &lt;a href=&quot;q://d?&amp;response&quot;&gt;response&lt;/a&gt; if it is not clear if anyone is present or listening, or if a telephone conversation may have been &lt;a href=&quot;q://d?&amp;disconnect&quot;&gt;disconnect&lt;/a&gt;ed.}}&lt;/li&gt;
&lt;ul&gt;&lt;li&gt; {{usex|&lt;b&gt;Hello&lt;/b&gt;? Is anyone there?|lang=en}}&lt;/li&gt;
&lt;li&gt; {{quote-book|year=1913|author={{w|Joseph C. Lincoln}}|chapter=7|title=[http://openlibrary.org/works/OL5535161W Mr. Pratt&#39;s Patients]|passage=I made a speaking trumpet of my hands and commenced to whoop &amp;#x201c;Ahoy!&amp;#x201d; and &amp;#x201c;&lt;b&gt;Hello!&lt;/b&gt;&amp;#x201d; at the top of my lungs. ... The Colonel woke up, and, after asking what in brimstone was the matter, opened his mouth and roared &amp;#x201c;Hi!&amp;#x201d; and &amp;#x201c;&lt;b&gt;Hello!&lt;/b&gt;&amp;#x201d; like the bull of Bashan.}}&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt; {{context|colloquial|lang=en}} {{non-gloss definition|Used &lt;a href=&quot;q://d?&amp;sarcastic&quot;&gt;sarcastic&lt;/a&gt;ally to imply that the person addressed or referred to has done something the speaker or writer considers to be &lt;a href=&quot;q://d?&amp;foolish&quot;&gt;foolish&lt;/a&gt;.}}&lt;/li&gt;
&lt;ul&gt;&lt;li&gt; {{usex|You just tried to start your car with your cell phone. &lt;b&gt;Hello&lt;/b&gt;?|lang=en}}&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt; {{non-gloss definition|An expression of &lt;a href=&quot;q://d?&amp;puzzlement&quot;&gt;puzzlement&lt;/a&gt; or &lt;a href=&quot;q://d?&amp;discovery&quot;&gt;discovery&lt;/a&gt;.}}&lt;/li&gt;
&lt;ul&gt;&lt;li&gt; {{usex|&lt;b&gt;Hello&lt;/b&gt;! What&amp;#x2019;s going on here?|lang=en}}&lt;/li&gt;
&lt;/ul&gt;
&lt;/ol&gt;
&lt;h4&gt;Usage notes&lt;/h4&gt;
&lt;ul&gt;&lt;li&gt; The greeting &lt;a href=&quot;q://d?&amp;hello&quot;&gt;hello&lt;/a&gt; is among the most generic and neutral in use. It may be heard in nearly all social situations and in nearly all walks of life, and is unlikely to cause offense.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;Quotations&lt;/h4&gt;
&lt;ul&gt;&lt;li&gt; {seeCites}&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;Synonyms&lt;/h4&gt;
&lt;ul&gt;&lt;li&gt; {{sense|greeting}}&lt;/li&gt;
&lt;ul&gt;&lt;li&gt; (AU, informal) &lt;a href=&quot;q://d?&amp;g%27day&quot;&gt;g&#39;day&lt;/a&gt;, &lt;a href=&quot;q://d?&amp;hey&quot;&gt;hey&lt;/a&gt;, &lt;a href=&quot;q://d?&amp;hi&quot;&gt;hi&lt;/a&gt;, &lt;/li&gt;
&lt;li&gt; (UK, informal) &lt;a href=&quot;q://d?&amp;hallo&quot;&gt;hallo&lt;/a&gt;, &lt;a href=&quot;q://d?&amp;hi&quot;&gt;hi&lt;/a&gt;, &lt;a href=&quot;q://d?&amp;hiya&quot;&gt;hiya&lt;/a&gt;, &lt;a href=&quot;q://d?&amp;ey+up&quot;&gt;ey up&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; (US, informal) &lt;a href=&quot;q://d?&amp;hallo&quot;&gt;hallo&lt;/a&gt;, &lt;a href=&quot;q://d?&amp;hey&quot;&gt;hey&lt;/a&gt;, &lt;a href=&quot;q://d?&amp;hi&quot;&gt;hi&lt;/a&gt;, &lt;a href=&quot;q://d?&amp;howdy&quot;&gt;howdy&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; (IE, informal) &lt;a href=&quot;q://d?&amp;how%27s+it+going&quot;&gt;how&#39;s it going&lt;/a&gt;, &lt;a href=&quot;q://d?&amp;hey&quot;&gt;hey&lt;/a&gt;, &lt;a href=&quot;q://d?&amp;hi&quot;&gt;hi&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; (SA, informal) &lt;a href=&quot;q://d?&amp;howzit&quot;&gt;howzit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; (slang) &lt;a href=&quot;q://d?&amp;wassup&quot;&gt;wassup&lt;/a&gt;, &lt;a href=&quot;q://d?&amp;what%27s+up&quot;&gt;what&#39;s up&lt;/a&gt;, &lt;a href=&quot;q://d?&amp;yo&quot;&gt;yo&lt;/a&gt;, &lt;a href=&quot;q://d?&amp;sup&quot;&gt;sup&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt; See also &lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;Antonyms&lt;/h4&gt;
&lt;ul&gt;&lt;li&gt; {{sense|greeting}} &lt;a href=&quot;q://d?&amp;bye&quot;&gt;bye&lt;/a&gt;, &lt;a href=&quot;q://d?&amp;goodbye&quot;&gt;goodbye&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;Derived terms&lt;/h4&gt;
&lt;ul&gt;&lt;li&gt; &lt;a href=&quot;q://d?&amp;hello+yourself%2C+and+see+how+you+like+it&quot;&gt;hello yourself, and see how you like it&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;See also&lt;/h4&gt;
&lt;ul&gt;&lt;li&gt; &lt;a href=&quot;q://d?&amp;%3ACategory%3AGreetings&quot;&gt;:Category:Greetings&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; {pedialite}&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Noun&lt;/h3&gt;
{{en-noun|s|helloes}}
&lt;ol&gt;&lt;li&gt; &amp;quot;&lt;a href=&quot;q://d?&amp;hello&quot;&gt;Hello&lt;/a&gt;!&amp;quot; or an equivalent greeting.&lt;/li&gt;
&lt;ul&gt;&lt;li&gt; {{quote-news|year=2007|date=April 29|author=Stephanie Rosenbloom|title=A Beautiful Day in the Neighborhood|work=New York Times|url=http://www.nytimes.com/2007/04/29/fashion/29condo.html|passage=In many new buildings, though, neighbors are venturing beyond tight-lipped &lt;b&gt;hellos&lt;/b&gt; at the mailbox.}}&lt;/li&gt;
&lt;/ul&gt;
&lt;/ol&gt;
&lt;h4&gt;Synonyms&lt;/h4&gt;
&lt;ul&gt;&lt;li&gt; &lt;a href=&quot;q://d?&amp;greeting&quot;&gt;greeting&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Verb&lt;/h3&gt;
{en-verb}
&lt;ol&gt;&lt;li&gt; {{context|transitive|lang=en}} To &lt;a href=&quot;q://d?&amp;greet&quot;&gt;greet&lt;/a&gt; with &amp;quot;hello&amp;quot;.&lt;/li&gt;
&lt;ul&gt;&lt;li&gt; &lt;b&gt;2013&lt;/b&gt;, Ivan Doig, &lt;em&gt;English Creek&lt;/em&gt; (page 139)&lt;/li&gt;
&lt;ul&gt;&lt;li&gt; I had to traipse around somewhat, &lt;b&gt;helloing&lt;/b&gt; people and being &lt;b&gt;helloed&lt;/b&gt;, before I spotted my mother and my father, sharing shade and a spread blanket with Pete and Marie Reese and Toussaint Rennie near the back of the park.&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;/ol&gt;
&lt;p&gt; &lt;a href=&quot;http://en.wiktionary.org/wiki/hello&quot;&gt;http://en.wiktionary.org/wiki/hello&lt;/a&gt;
&lt;/body&gt;&lt;/html&gt;&quot;

In mystring I am trying to remove

&lt;h1&gt;&lt;a href=&quot;q://d?EN&amp;hello&quot;&gt;hello&lt;/a&gt;&lt;/h1&gt;

I tried with : https://stackoverflow.com/a/32005143/1083093 ... this dosen't work since I am trying to remove only h1 tags and content between them

答案1

得分: 2

public static void main(String[] args) {
    String mystring = "<html><head><meta name=\"viewport\" content=\"width=device-width\"></head><body><h1><a href=\"q://d?EN&amp;hello\">hello</a></h1>";
    System.out.println(removeH1(mystring));
}

private static String removeH1(String mystring) {
    while (mystring.contains("<h1>")) {
        mystring = mystring.substring(0, mystring.indexOf("<h1>")) + mystring.substring(mystring.indexOf("</h1>") + 5);
    }
    return mystring;
}
英文:
public static void main(String[] args) {
String mystring = &quot;&lt;html&gt;&lt;head&gt;&lt;meta name=\&quot;viewport\&quot; content=\&quot;width=device-width\&quot;&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;&lt;a href=\&quot;q://d?EN&amp;hello\&quot;&gt;hello&lt;/a&gt;&lt;/h1&gt;&quot;;
System.out.println(removeH1(mystring));
}
private static String removeH1(String mystring) {
while (mystring.contains(&quot;&lt;h1&gt;&quot;)) {
mystring = mystring.substring(0, mystring.indexOf(&quot;&lt;h1&gt;&quot;)) + mystring.substring(mystring.indexOf(&quot;&lt;/h1&gt;&quot;) + 5);
}
return mystring;
}

Pretty straightforward, you may comment if you have any questions.

答案2

得分: 1

这将删除所有的&lt;h1&gt;标签以及标签之间的内容:

str = str.replaceAll("&lt;h1&gt;.*?&lt;/h1&gt;", "");
英文:

This will delete all &lt;h1&gt; tags and the content inbetween:

str = str.replaceAll(&quot;&lt;h1&gt;.*?&lt;/h1&gt;&quot;, &quot;&quot;);

huangapple
  • 本文由 发表于 2020年4月7日 06:09:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/61069739.html
匿名

发表评论

匿名网友

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

确定