Java 8 正则表达式查找字符串第三次冒号之前的内容
使用正则表达式来查找字符串第三次冒号之前的全部内容。可以使用以下代码实现:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String str = "abc:def:ghi:jkl:mno:pqr:stu:vwx:yz";
String regex = "^((?:[^:]*:){3}[^:]*)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
if (matcher.find()) {
String result = matcher.group(1);
System.out.println(result);
}
}
}
输出结果为:abc:def:ghi
原文地址: https://www.cveoy.top/t/topic/pxYB 著作权归作者所有。请勿转载和采集!