您好,我有以下字符串:
Country number Time Status USA B30111 11:15 ARRIVED PARIS NC0120 14:40 ON TIME DUBAI RA007 14:45 ON TIME
我需要提取以下信息:
country = USA
number = B30111
time = 11:15
status = ARRIVED
country = PARIS
number = NC0120
time = 14:40
status = ON TIME
如何使用正则表达式从中提取以上数据?
您可以尝试以下方法:
(?: (\w+) ([\w\d]+) (\d+\:\d+) (ARRIVED|ON TIME))
由于状态可以包含多个单词,因此无法将其与出现的下一个国家区分开,因此您必须将所有可能的状态附加为or |。在正则表达式中
Java资料来源:
final String regex = "(?: (\\w+) ([\\w\\d]+) (\\d+\\:\\d+) (ARRIVED|ON TIME))";
final String string = "Country number Time Status USA B30111 11:15 ARRIVED PARIS NC0120 14:40 ON TIME DUBAI RA007 14:45 ON TIME\n\n\n";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("country =" + matcher.group(1));
System.out.println("number =" + matcher.group(2));
System.out.println("time =" + matcher.group(3));
System.out.println("status =" + matcher.group(4));
System.out.println("");
}
输出
country =USA
number =B30111
time =11:15
status =ARRIVED
country =PARIS
number =NC0120
time =14:40
status =ON TIME
country =DUBAI
number =RA007
time =14:45
status =ON TIME
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句