gpt4 book ai didi

java - 正则表达式从字符串中获取特定值

转载 作者:行者123 更新时间:2023-12-02 12:36:12 25 4
gpt4 key购买 nike

下面的结果是一个字符串输出。

test179901034102 00:00:00:00:00:01 Open
test179901083723 00:00:00:00:00:01 Open
test179901153595 00:00:00:00:00:01 Open
test179901187836 00:00:00:00:00:01 Open

WebElement element = driver.findElement(By.xpath("//div[contains(@class,'filteredTable')]"));
System.out.println( element.getText()); // returns the above string

我的目的是获取值 test179901034102 、 test179901083723 etc 作为与字符串分开的列表。

如何使用正则表达式从下面的字符串中获取值(`test179901034102,test179901083723)

test179901034102 00:00:00:00:00:01 Open
test179901083723 00:00:00:00:00:01 Open
test179901153595 00:00:00:00:00:01 Open
test179901187836 00:00:00:00:00:01 Open

最佳答案

您可以使用^[^]+

示例:

String s = "test179901034102 00:00:00:00:00:01 Open\n" +
"test179901083723 00:00:00:00:00:01 Open\n" +
"test179901153595 00:00:00:00:00:01 Open\n" +
"test179901187836 00:00:00:00:00:01 Open";

Pattern pattern = Pattern.compile("^[^ ]+", Pattern.MULTILINE);
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
System.out.println(matcher.group(0));
}

参见:https://regex101.com/r/aZYgvC/1

关于java - 正则表达式从字符串中获取特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45125544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com