0
[introduction][position]Lead Researcher and Research Manager[/position] in the [affiliation]Web Search and Mining Group, Microsoft Research[/affiliation]</b>.

I am a [position]lead researcher[/position] at [affiliation]Microsoft Research[/affiliation]. I am also [position]adjunct professor[/position] of [affiliation]Peking University[/affiliation], [affiliation]Xian Jiaotong University[/affiliation] and [affiliation]Nankai University[/affiliation].

I joined [affiliation]Microsoft Research[/affiliation] in June 2001. Prior to that, I worked at the Research Laboratories of NEC Corporation.

I obtained a [bsdegree]B.S.[/bsdegree] in [bsmajor]Electrical Engineering[/bsmajor] from [bsuniv]Kyoto University[/bsuniv] in [bsdate]1988[/bsdate] and a [msdegree]M.S.[/msdegree] in [msmajor]Computer Science[/msmajor] from [msuniv]Kyoto University[/msuniv] in [msdate]1990[/msdate]. I earned my [phddegree]Ph.D.[/phddegree] in [phdmajor]Computer Science[/phdmajor] from the [phduniv]University of Tokyo[/phduniv] in [phddate]1998[/phddate].

I am interested in [interests]statistical learning[/interests], [interests]natural language processing[/interests], [interests]data mining, and information retrieval[/interests].[/introduction]

我可以从上面的段落中删除所有标签:

String stripped = html.replaceAll("\\[.*?\\]", "");

但我想在段落中保留三对标签,它们是[bsuniv][/bsuniv],[msuniv][/msuniv][phduniv][/phduniv]. 换句话说,我不想去掉那些包含关键字“univ”的标签。我找不到重写正则表达式的便捷方法。有人帮我吗?

4

1 回答 1

1

您可以在此处使用negative-look ahead断言:-

str = str.replaceAll("\\[(.(?!univ))*?\\]", "");

或者: -

str = str.replaceAll("\\[((?!univ).)*?\\]", "");

它们都将为您提供所需的输出。只有一个区别——

  • 第一个对当前字符进行负前瞻,如果后面没有univ,则移动到下一个字符。
  • 第二个对每个字符之前的空字符串进行负前瞻,如果后面没有univ,则继续匹配单个字符。
于 2012-12-17T05:41:40.400 回答