gpt4 book ai didi

java - 正则表达式:删除除包含关键字 "univ"之外的所有标签

转载 作者:行者123 更新时间:2023-12-01 14:59:19 26 4
gpt4 key购买 nike

[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”的标签。我找不到重写正则表达式的便捷方法。有人帮帮我吗?

最佳答案

您可以在此处使用否定前瞻断言:-

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

或者:-

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

它们都会给你想要的输出。只有一处不同 -

  • 第一个字符对当前字符进行负向前查找,如果后面没有 univ,它将移至下一个字符。
  • 第二个对每个字符之前的空字符串进行否定前瞻,如果后面没有 univ,它将继续匹配单个字符。

关于java - 正则表达式:删除除包含关键字 "univ"之外的所有标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13908716/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com