[英]Finding the 1st and 2nd occurrence of a regex in a string in Java
If I have a string such as如果我有一个字符串,例如
First timing is 10:10:30, second timing is 11:12:45, third is 12:14:25
How do I get only the first and second occurrence of timing that is formatted to show only the minutes and seconds?如何仅获取格式化为仅显示分钟和秒的第一次和第二次出现的时间?
The end result should be 2 strings for example,例如,最终结果应该是 2 个字符串,
string1 = 10:10
string2 = 11:12
Thank you.谢谢你。
var regEx = /timing[^\\d]+(\\d+:\\d+)/ig; var text = 'First timing is 10:10:30, second timing is 11:12:45, third is 12:14:25' var match = regEx.exec(text); while(match !== null) { console.log(match[1]); match = regEx.exec(text); }
public static void main(String[] args) {
String myString = "First timing is 10:40:30, second timing is 11:12:45,
third is 12:14:25";
myString = myString.replaceAll("[a-zA-z]","");
String[] numbers = myString.trim().split("\\s*,\\s*");
for(int i=0;i<numbers.length;i++){
System.out.println("string"+(i+1)+" = "+numbers[i].substring(0,numbers[i].length()-3));
}
}
Output输出string1 = 10:40 string2 = 11:12 string3 = 12:14
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.