简体   繁体   English

如何删除<h1>来自 html 字符串 java 的标签</h1>

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

I have a string我有一个字符串

String mystring = "<html><head><meta name="viewport" content="width=device-width"></head><body><h1><a href="q://d?EN&hello">hello</a></h1>
<p>
<h3>Alternative forms</h3>
<ul><li> <a href="q://d?&hallo">hallo</a></li>
<li> <a href="q://d?&hilloa">hilloa</a> (obsolete)</li>
<li> <a href="q://d?&hullo">hullo</a> (UK)</li>
</ul>

<h3>Etymology</h3>
<a href="q://d?&Hello">hello</a> (first attested in 1833), from <a href="q://d?&holla">holla</a>, <a href="q://d?&hollo">hollo</a> (attested 1588). This variant of <a href="q://d?&hallo">hallo</a> 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 <em><a href="q://d?&eala">&#x113;al&#x101;</a></em>, such as <em><a href="q://d?&hela">h&#x113;l&#x101;</a></em>, which was used colloquially in that time similarly to how &quot;hey&quot; or &quot;hi&quot; is used nowadays. Thus, equivalent to a compound of <em><a href="q://d?&hey">hey</a></em> and <em><a href="q://d?&lo">lo</a></em>.Possibly influenced by the lang:goh and lang:osx verb <a href="q://d?&halon">halon</a>, <a href="q://d?&holon">holon</a> (&quot;to bring something, to gather&quot;), akin to English <a href="q://d?&hale">hale</a> or <a href="q://d?&hail">hail</a>. More at {{l/en|hallo}}.
<h3>Pronunciation</h3>
<ul><li> {{a|UK}} IPA: /h&#x259;&#x2c8;l&#x259;&#x28a;&#x32f;/, /h&#x25b;&#x2c8;l&#x259;&#x28a;&#x32f;/</li>
<li> {{a|US}} {{enPR|h&#x115;-l&#x14d;&#x27;|h&#x259;-l&#x14d;&#x27;}}, IPA: /h&#x25b;&#x2c8;lo&#x28a;&#x32f;/, /h&#x259;&#x2c8;lo&#x28a;&#x32f;/</li>
<li> {{audio|En-uk-hello.ogg|Audio (UK)|lang=en}}</li>
</ul>
{|class=&quot;wikitable&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}}||}
<ul><li> {{rhymes|&#x259;&#x28a;|lang=en}}</li>
</ul>

<h3>Interjection</h3>
{en-interj}
<ol><li> {{non-gloss definition|A <a href="q://d?&greeting">greeting</a> (<a href="q://d?&salutation">salutation</a>) said when <a href="q://d?&meet">meet</a>ing someone or <a href="q://d?&acknowledge">acknowledging</a> someone&#x2019;s <a href="q://d?&arrival">arrival</a> or <a href="q://d?&presence">presence</a>.}}</li>
<ul><li> {{usex|<b>Hello,</b> everyone.|lang=en}}</li>
</ul>
<li> {{non-gloss definition|A greeting used when <a href="q://d?&answer">answer</a>ing the <a href="q://d?&telephone">telephone</a>.}}</li>
<ul><li> {{usex|<b>Hello</b>? How may I help you?|lang=en}}</li>
</ul>
<li> {{non-gloss definition|A call for <a href="q://d?&response">response</a> if it is not clear if anyone is present or listening, or if a telephone conversation may have been <a href="q://d?&disconnect">disconnect</a>ed.}}</li>
<ul><li> {{usex|<b>Hello</b>? Is anyone there?|lang=en}}</li>
<li> {{quote-book|year=1913|author={{w|Joseph C. Lincoln}}|chapter=7|title=[http://openlibrary.org/works/OL5535161W Mr. Pratt's Patients]|passage=I made a speaking trumpet of my hands and commenced to whoop &#x201c;Ahoy!&#x201d; and &#x201c;<b>Hello!</b>&#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 &#x201c;Hi!&#x201d; and &#x201c;<b>Hello!</b>&#x201d; like the bull of Bashan.}}</li>
</ul>
<li> {{context|colloquial|lang=en}} {{non-gloss definition|Used <a href="q://d?&sarcastic">sarcastic</a>ally to imply that the person addressed or referred to has done something the speaker or writer considers to be <a href="q://d?&foolish">foolish</a>.}}</li>
<ul><li> {{usex|You just tried to start your car with your cell phone. <b>Hello</b>?|lang=en}}</li>
</ul>
<li> {{non-gloss definition|An expression of <a href="q://d?&puzzlement">puzzlement</a> or <a href="q://d?&discovery">discovery</a>.}}</li>
<ul><li> {{usex|<b>Hello</b>! What&#x2019;s going on here?|lang=en}}</li>
</ul>
</ol>

<h4>Usage notes</h4>
<ul><li> The greeting <a href="q://d?&hello">hello</a> 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.</li>
</ul>

<h4>Quotations</h4>
<ul><li> {seeCites}</li>
</ul>

<h4>Synonyms</h4>
<ul><li> {{sense|greeting}}</li>
<ul><li> (AU, informal) <a href="q://d?&g%27day">g'day</a>, <a href="q://d?&hey">hey</a>, <a href="q://d?&hi">hi</a>, </li>
<li> (UK, informal) <a href="q://d?&hallo">hallo</a>, <a href="q://d?&hi">hi</a>, <a href="q://d?&hiya">hiya</a>, <a href="q://d?&ey+up">ey up</a></li>
<li> (US, informal) <a href="q://d?&hallo">hallo</a>, <a href="q://d?&hey">hey</a>, <a href="q://d?&hi">hi</a>, <a href="q://d?&howdy">howdy</a></li>
<li> (IE, informal) <a href="q://d?&how%27s+it+going">how's it going</a>, <a href="q://d?&hey">hey</a>, <a href="q://d?&hi">hi</a></li>
<li> (SA, informal) <a href="q://d?&howzit">howzit</a></li>
<li> (slang) <a href="q://d?&wassup">wassup</a>, <a href="q://d?&what%27s+up">what's up</a>, <a href="q://d?&yo">yo</a>, <a href="q://d?&sup">sup</a></li>
</ul>
<li> See also </li>
</ul>

<h4>Antonyms</h4>
<ul><li> {{sense|greeting}} <a href="q://d?&bye">bye</a>, <a href="q://d?&goodbye">goodbye</a></li>
</ul>

<h4>Derived terms</h4>
<ul><li> <a href="q://d?&hello+yourself%2C+and+see+how+you+like+it">hello yourself, and see how you like it</a></li>
</ul>

<h4>See also</h4>
<ul><li> <a href="q://d?&%3ACategory%3AGreetings">:Category:Greetings</a></li>
<li> {pedialite}</li>
</ul>

<h3>Noun</h3>
{{en-noun|s|helloes}}
<ol><li> &quot;<a href="q://d?&hello">Hello</a>!&quot; or an equivalent greeting.</li>
<ul><li> {{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 <b>hellos</b> at the mailbox.}}</li>
</ul>
</ol>

<h4>Synonyms</h4>
<ul><li> <a href="q://d?&greeting">greeting</a></li>
</ul>

<h3>Verb</h3>
{en-verb}
<ol><li> {{context|transitive|lang=en}} To <a href="q://d?&greet">greet</a> with &quot;hello&quot;.</li>
<ul><li> <b>2013</b>, Ivan Doig, <em>English Creek</em> (page 139)</li>
<ul><li> I had to traipse around somewhat, <b>helloing</b> people and being <b>helloed</b>, 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.</li>
</ul>
</ul>
</ol>
<p> <a href="http://en.wiktionary.org/wiki/hello">http://en.wiktionary.org/wiki/hello</a>
</body></html>"

In mystring I am trying to removemystring我试图删除

<h1><a href="q://d?EN&hello">hello</a></h1>

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我试过https://stackoverflow.com/a/32005143/1083093 ...这不起作用,因为我试图只删除它们之间的h1标签和内容

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&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;
}

Pretty straightforward, you may comment if you have any questions.很简单,有问题可以评论。

This will delete all <h1> tags and the content inbetween:这将删除所有<h1>标记和其间的内容:

str = str.replaceAll("<h1>.*?</h1>", "");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM