gpt4 book ai didi

Java 正则表达式 : need one regex to match all the formats specified

转载 作者:行者123 更新时间:2023-12-02 06:51:35 24 4
gpt4 key购买 nike

日志文件中这些模式在一行中多次出现。例如该文件可能看起来像

dsads utc-hour_of_year:2013-07-30T17 jdshkdsjhf utc-week_of_year:2013-W31 dskjdskf
utc-week_of_year:2013-W31 dskdsld fdsfd
dshdskhkds utc-month_of_year:2013-07 gfdkjlkdf

我想用“Y”替换所有日期特定信息

我尝试过:ReplaceAll("_year:.*\s", "_year:Y ");` 但由于 .* 的贪婪匹配,它会删除第一次替换后发生的所有内容

dsads utc-hour_of_year:Y
utc-week_of_year:Y
dshdskhkds utc-month_of_year:Y

但预期的结果是:

dsads utc-hour_of_year:Y jdshkdsjhf utc-week_of_year:Y dskjdskf
utc-week_of_year:Y dskdsld fdsfd
dshdskhkds utc-month_of_year:Y gfdkjlkdf

最佳答案

尝试使用不情愿的量词:_year:.*?\s

.replaceAll("_year:.*?\\s", "_year:Y ")
<小时/>
System.out
.println("utc-hour_of_year:2013-07-30T17 dsfsdgfsgf utc-week_of_year:2013-W31 dsfsdgfsdgf"
.replaceAll("_year:.*?\\s", "_year:Y "));
utc-hour_of_year:Y dsfsdgfsgf utc-week_of_year:Y dsfsdgfsdgf

关于Java 正则表达式 : need one regex to match all the formats specified,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17954136/

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