Java 使用正则表达式从字符串中删除特定XML标记的所有实例

Java 使用正则表达式从字符串中删除特定XML标记的所有实例,java,regex,replace,regex-lookarounds,Java,Regex,Replace,Regex Lookarounds,假设我的文本包含以下标记: <ref name="name">Some text</ref> <ref name="adasd"/> <!--Hello world--> 假设ref name不重要,您可以尝试: &lt;(?:ref.*?(?:&gt;.*?&lt;\/ref|\/)&gt;|!--&gt;.*?&lt;--&gt;) “使用正则表达式从字符串中删除特定XML标记的所有实

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

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

假设ref name不重要,您可以尝试:

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

“使用正则表达式从字符串中删除特定XML标记的所有实例”-不,这不是正则表达式的工作:使用XML解析器并正确地执行它!我从XML的内容部分获取此文本。引用标记不会转换为XML格式,而是保持为and&rt;所以,我是这样删除它的。@purudpd在您的摘录中我们看不到任何
,但是
。很抱歉,这是由于堆栈溢出造成的。请同样对待他们。我将编辑问题我编辑了你的问题,因此文本看起来更像你描述的方式。如果您不喜欢,可以随意回滚。由于某些原因,它在java中不起作用。让我再试一次。可能是因为我的正则表达式中的转义
\/
?试着用
/
替换
\/
。我正在写:text=text.replaceAll((?:ref.*?(?:.*/ref |/)|!--.-),“”);它似乎不起作用,但在Regex101上运行良好不确定,但参考java文档,您似乎也应该将
(?:
替换为
(?
)。但看起来这不是重点。我很惊讶您必须找一个比我更好的java编码器。
> [[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:
&lt;(?:ref.*?(?:&gt;.*?&lt;\/ref|\/)&gt;|!--&gt;.*?&lt;--&gt;)