我有一些带有模式的字符串:
15764_Coordinator/Principal Investigator, Curator;44504_Database Manager, Architect;43401_Scientific Expert;43701_Scientific Expert;
此模式中的分隔符是分号;
。如果我有任何起始编号说44504
,我想删除以此编号开头的字符串部分,直到分号;
。删除后的字符串为:
15764_Coordinator/Principal Investigator, Curator;43401_Scientific Expert;43701_Scientific Expert;
我怎样才能做到这一点?
答案 0 :(得分:3)
您需要使用string.replaceAll
功能。
string.replaceAll("(?m)(?<=^|;)44504[^;]*;", "")
(?m)
多行修改器。当您处理多行输入时,正则表达式包含锚点( ^
,$
)
(?<=^|;)
正面的后视,断言匹配必须以分号或行的开头开头。
[^;]*
否定了符合任何字符但不符合;
,零次或多次的字符类。
示例:强>
String s = "15764_Coordinator/Principal Investigator, Curator;44504_Database Manager, Architect;43401_Scientific Expert;43701_Scientific Expert;";
System.out.println(s.replaceAll("(?<=^|;)44504[^;]*;", ""));
输出:
15764_Coordinator/Principal Investigator, Curator;43401_Scientific Expert;43701_Scientific Expert;
答案 1 :(得分:1)
试试这个..
string.replaceAll("(?<=\;)44504.*?\;", "")