使用regex从字符串中删除特定XML标记的所有实例

时间:2017-11-21 07:22:02

标签: java regex replace regex-lookarounds

假设我的文字包含以下标记:

<ref name="name">Some text</ref>
<ref name="adasd"/>
<!--Hello world-->

如何从文本中完全删除所有此类实例。我不想要标签之间的文字。 我试过这个:

text = text.replaceAll("(?s)<ref>(.*?)</ref>"," ");

请在下面的文字中试用你的正则表达式:

> [[File:Bakunin.png|thumb|upright|Collectivist anarchist [[Mikhail
> Bakunin]] opposed the [[Marxist]] aim of [[dictatorship of the
> proletariat]] in favour of universal rebellion, and allied himself
> with the federalists in the First International before his expulsion
> by the Marxists.&lt;ref name=bbc/&gt;]]In Europe, harsh reaction
> followed the [[revolutions of 1848]], during which ten countries had
> experienced brief or long-term social upheaval as groups carried out
> nationalist uprisings. After most of these attempts at systematic
> change ended in failure, conservative elements took advantage of the
> divided groups of socialists, anarchists, liberals, and nationalists,
> to prevent further revolt.&lt;ref&gt; &lt;/ref&gt; In Spain Ramón de
> la Sagra established the anarchist journal ''El Porvenir'' in La
> Coruña in 1845 which was inspired by Proudhon´s ideas.&lt;ref
> name="britannica.com"&gt; &lt;/ref&gt; The [[Catalan people|Catalan]]
> politician [[Francesc Pi i Margall]] became the principal translator
> of Proudhon's works into Spanish&lt;ref&gt;[[George Woodcock]].
> ''Anarchism: a history of libertarian movements''. Pg. 357&lt;/ref&gt;
> and later briefly became president of Spain in 1873 while being the
> leader of the Democratic Republican Federal Party. According to George
> Woodcock "These translations were to have a profound and lasting
> effect on the development of Spanish anarchism after 1870, but before
> that time Proudhonian ideas, as interpreted by Pi, already provided
> much of the inspiration for the federalist movement which sprang up in
> the early 1860's."&lt;ref&gt;George Woodcock. ''Anarchism: a history
> of libertarian movements''. Pg. 357&lt;/ref&gt; According to the
> ''[[Encyclopædia Britannica]]'' "During the Spanish revolution of
> 1873, Pi y Margall [[Cantonal Revolution|attempted to establish a
> decentralised, or "cantonalist," political system]] on Proudhonian
> lines."&lt;ref name="britannica.com"/&gt;In 1864 the [[International
> Workingmen's Association]] (sometimes called the "First
> International") united diverse revolutionary currents including French
> followers of Proudhon,&lt;ref&gt; &lt;/ref&gt;
> [[Blanquism|Blanquists]], [[Philadelphes]], English trade unionists,
> socialists and [[social democrats]]. Due to its links to active
> workers' movements, the International became a significant
> organisation. [[Karl Marx]] became a leading figure in the
> International and a member of its General Council. Proudhon's
> followers, the mutualists, opposed Marx's [[state socialism]],
> advocating political [[abstentionism]] and small property
> holdings.&lt;ref&gt; &lt;/ref&gt;&lt;ref&gt; &lt;/ref&gt; Woodcock
> also reports that the American individualist anarchists [[Lysander
> Spooner]] and [[William B. Greene]] had been members of the [[First
> International]].&lt;ref name="Woodcock"&gt; &lt;/ref&gt; In 1868,
> following their unsuccessful participation in the [[League of Peace
> and Freedom]] (LPF), Russian revolutionary Mikhail Bakunin and his
> collectivist anarchist associates joined the First International
> (which had decided not to get involved with the LPF).&lt;ref&gt;
> &lt;/ref&gt; They allied themselves with the [[federalist]] socialist
> sections of the International,&lt;ref&gt; &lt;/ref&gt; who advocated
> the revolutionary overthrow of the state and the collectivisation of
> property.At first, the collectivists worked with the Marxists to push
> the First International in a more revolutionary socialist direction.
> Subsequently, the International became polarised into two camps, with
> Marx and Bakunin as their respective figureheads.&lt;ref&gt;
> &lt;/ref&gt; [[Mikhail Bakunin]] characterised Marx's ideas as
> [[centralism|centralist]] and predicted that, if a Marxist party came
> to power, its leaders would simply take the place of the [[ruling
> class]] they had fought against.&lt;ref name="bakuninmarx"
> /&gt;&lt;ref&gt; &lt;/ref&gt; Anarchist historian George Woodcock
> reports that "The annual Congress of the International had not taken
> place in 1870 owing to the outbreak of the Paris Commune, and in 1871
> the General Council called only a special conference in London. One
> delegate was able to attend from Spain and none from Italy, while a
> technical excuse – that they had split away from the Fédération
> Romande – was used to avoid inviting Bakunin's Swiss supporters. Thus
> only a tiny minority of anarchists was present, and the General
> Council's resolutions passed almost unanimously. Most of them were
> clearly directed against Bakunin and his followers."&lt;ref
> name="Anarchism 1962"&gt;[[George Woodcock]]. ''Anarchism: A History
> of Libertarian Ideas and Movements'' (1962)&lt;/ref&gt; In 1872, the
> conflict climaxed with a final split between the two groups at the
> [[Hague Congress (1872)|Hague Congress]], where Bakunin and [[James
> Guillaume]] were expelled from the International and its headquarters
> were transferred to New York. In response, the federalist sections
> formed their own International at the [[Anarchist St. Imier
> International|St. Imier Congress]], adopting a revolutionary anarchist
> programme.&lt;ref name=Graham-05&gt;Graham, Robert
> '[http://www.blackrosebooks.net/anarism1.htm ''Anarchism''] (Montreal:
> Black Rose Books 2005) ISBN 1-55164-251-4.&lt;/ref&gt;The [[Paris
> Commune]] was a government that briefly ruled Paris from 18 March
> (more formally, from 28 March) to 28 May 1871. The Commune was the
> result of an uprising in Paris after France was defeated in the
> Franco-Prussian War. Anarchists participated actively in the
> establishment of the Paris Commune. They included [[Louise Michel]],
> the Reclus brothers, and [[Eugene Varlin]] (the latter murdered in the
> repression afterwards). As for the reforms initiated by the Commune,
> such as the re-opening of workplaces as co-operatives, anarchists can
> see their ideas of associated labour beginning to be
> realised&amp;nbsp;... Moreover, the Commune's ideas on federation
> obviously reflected the influence of Proudhon on French radical ideas.
> Indeed, the Commune's vision of a communal France based on a
> federation of delegates bound by imperative mandates issued by their
> electors and subject to recall at any moment echoes Bakunin's and
> Proudhon's ideas (Proudhon, like Bakunin, had argued in favour of the
> "implementation of the binding mandate" in 1848&amp;nbsp;... and for
> federation of communes). Thus both economically and politically the
> Paris Commune was heavily influenced by anarchist ideas.&lt;ref&gt;
> &lt;/ref&gt; George Woodcock states:

1 个答案:

答案 0 :(得分:1)

假设引用名称没有重要性,您可以尝试:

&lt;(?:ref.*?(?:&gt;.*?&lt;\/ref|\/)&gt;|!--&gt;.*?&lt;--&gt;)

Demo