我有一些带图案的字符串:
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;我怎样才能做到这一点?
发布于 2015-03-02 12:53:26
您需要使用string.replaceAll函数。
string.replaceAll("(?m)(?<=^|;)44504[^;]*;", "")(?m)多行修饰符。在处理包含锚的多行输入时(^,__,$),这是必要的。(?<=^|;)正向查找,它断言匹配之前必须有一个分号或行的开始。[^;]*否定字符类,它匹配任何字符,但不匹配;,0次或多次。示例:
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;发布于 2015-03-02 12:55:21
试试这个..。
string.replaceAll("(?<=\;)44504.*?\;", "")https://stackoverflow.com/questions/28810262
复制相似问题