[英]Java regular expressions starts and ends with and contains
I have a file that I need to use regex to replace a specific character. 我有一个文件,需要使用正则表达式替换特定字符。 I have strings of the following format: 我有以下格式的字符串:
1234 4215 "aaa.bbb" 5215 1524
and I need to replace the periods with colons. 我需要用冒号代替句号。 I know that these periods are always contained within quotation marks, so I need a regex that finds a substring that starts with '"', ends with '"', and contains "." 我知道这些时间段总是包含在引号中,因此我需要一个正则表达式来查找一个以“”开头,以“”结尾,并包含“”的子字符串。 and replace the "." 并替换为“。” with ":". 与“:”。 Could someone shed some light? 有人可以照亮吗?
You can use: 您可以使用:
str = str.replaceAll("\\.(?!(([^"]*"){2})*[^"]*$)", ":");
This regex will find dots if those are inside double quotes by using a lookahead to make sure there are NOT even number of quotes after the dot. 如果正则表达式可以通过使用超前查找来确保圆点后没有双引号,则该正则表达式将找到圆点。
After thinking about it, your question says "period(s)" possibly more than one period in double quotes. 考虑一下之后,您的问题用双引号括起来的“句号”可能不止一个句号。
Here's a way to cover that scenario 这是解决这种情况的一种方法
public static void main(String[] args) throws Exception {
String str = "1234 \"aaa.bbb\" \"a.aa.b.bb\" 5215 1524 \"12.345.123\" \".sage.\" \".afwe\" \"....\"";
// Find all substrings in double quotes
Matcher matcher = Pattern.compile("\"(.*?)\"").matcher(str);
while (matcher.find()) {
// Extract the match
String match = matcher.group(1);
// Replace all the periods with colons
match = match.replaceAll("\\.", ":");
// Replace the original matched group with the new string
str = str.replace(matcher.group(1), match);
}
System.out.println(str);
}
Results: 结果:
1234 "aaa:bbb" "a:aa:b:bb" 5215 1524 "12:345:123" ":sage:" ":afwe" "::::"
And after testing @anubhava pattern, his produces the same results so more credit to him for simplicity (+1). 并且在测试@anubhava模式之后,他产生了相同的结果,因此为简单起见(+1)而更多地归功于他。
You can try this pattern in a String.replaceAll()
您可以在String.replaceAll()
尝试这种模式
"\"([^\\.]*?)(\\.)([^\\.]*?)\""
With a replacement of 用替换
"\"$1:$3\""
This essentially captures the contents, between double quotes, into groups (1-3). 这实际上将双引号之间的内容捕获为组(1-3)。
and replaces it with "{Group 1}:{Group 3}" 并将其替换为“ {Group 1}:{Group 3}”
public static void main(String[] args) throws Exception {
String str = "1234 4215 \"aaa.bbb\" 5215 1524 \"12345.123\" \"sage.\" \".afwe\" \".\"";
System.out.println(str.replaceAll("\"([^\\.]*?)(\\.)([^\\.]*?)\"", "\"$1:$3\""));
}
Results: 结果:
1234 4215 "aaa:bbb" 5215 1524 "12345:123" "sage:" ":afwe" ":"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.