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

如何删除<;h1>;来自html字符串java的标记,java,Java,我有一根绳子 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&g

我有一根绳子

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>"
我试过了:。。。这不起作用,因为我试图只删除
h1
标记和它们之间的内容

公共静态void main(String[]args){
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;
}
字符串mystring=“”; System.out.println(removeH1(mystring)); } 私有静态字符串removeH1(字符串mystring){ while(mystring.contains(“”){ mystring=mystring.substring(0,mystring.indexOf(“”)+mystring.substring(mystring.indexOf(“”+5); } 返回mystring; }

非常简单,如果您有任何问题,您可以发表评论。

这将删除所有
标记以及它们之间的内容:

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

太好了。。。你试过什么?当然,有你的网站经验的人知道,社区通常希望在发布之前尽最大努力。所有H1标签,特别是这一个,或者所有H1标签看起来都是某种方式?是的,你试过自己解决这个问题吗?(例如使用XML解析器、JSoup、regex等)(提示regex+html=不推荐)@roddyOfFrozenpeas。。。删除h1标记之间的数据,包括我没有要求的标记。所有的h1,特别是这一个,或标签,看起来有一定的方式)有一定的结构?向上投票,因为这也起作用。。。。。但既然马克的回答很优雅。。。我添加了绿色的记号标记。您如何确保字符数少于10?最好使用不情愿的量词
“*?”
(而不是贪婪的一个或计数)甚至更好。。编辑为使用不情愿的量词而不是计数。
str = str.replaceAll("<h1>.*?</h1>", "");