java 正则表达式,替换双引号之间的所有逗号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1658538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regular expression, replace all commas between double quotes
提问by roflwaffle
I have this string:
我有这个字符串:
1001,"Fitzsimmons, Des Marteau, Beale and Nunn",109,"George","COD","Standard",,109,8/14/1998 8:50:02
What regular expression would I use to replace the commas in the
"Fitzsimmons, Des Marteau, Beale and Nunn"with a pipe |so it is:
我会用什么正则表达式来用"Fitzsimmons, Des Marteau, Beale and Nunn"管道替换逗号,
|所以它是:
"Fitzsimmons| Des Marteau| Beale and Nunn"
Should have clarified, I am doing a split on this string using the commas, so I want "Fitzsimmons, Des Marteau, Beale and Nunn"to be a string. I plan to replace the |with a comma after I have split it.
应该澄清一下,我正在使用逗号对这个字符串进行拆分,所以我想"Fitzsimmons, Des Marteau, Beale and Nunn"成为一个字符串。我打算|在拆分后用逗号替换它。
回答by jheddings
回答by Aif
I have tried to use StringTokenizerbut it didn't work well, so here is a code which seems to do what you want:
我曾尝试使用StringTokenizer但效果不佳,所以这里有一段代码似乎可以满足您的需求:
import java.util.*;
public class JTest
{
public static void main(String[] args)
{
String str = "1001,\"Fitzsimmons, Des Marteau, Beale and Nunn\",109,\"George\",\"COD\",\"Standard\",,109,8/14/1998 8:50:02";
String copy = new String();
boolean inQuotes = false;
for(int i=0; i<str.length(); ++i)
{
if (str.charAt(i)=='"')
inQuotes = !inQuotes;
if (str.charAt(i)==',' && inQuotes)
copy += '|';
else
copy += str.charAt(i);
}
System.out.println(str);
System.out.println(copy);
}
}
回答by Ning120
Hey Brandon you can easily do this with RE by using look behind and look ahead. see the code below
嘿布兰登,您可以通过使用向后展望和展望未来轻松地使用 RE 做到这一点。看下面的代码
String cvsString = "1001,\"Fitzsimmons, Des Marteau, Beale and Nunn\",109,\"George\",\"COD\",\"Standard\",,109,8/14/1998 8:50:02";
String rePattern = "(?<=\")([^\"]+?),([^\"]+?)(?=\")";
// first replace
String oldString = cvsString;
String resultString = cvsString.replaceAll(rePattern, "|");
// additional repalces until until no more changes
while (!resultString.equalsIgnoreCase(oldString)){
oldString = resultString;
resultString = resultString.replaceAll(rePattern, "|");
}
result string will be 1001,"Fitzsimmons| Des Marteau| Beale and Nunn",109,"George","COD","Standard",,109,8/14/1998 8:50:02
结果字符串将是 1001,"Fitzsimmons| Des Marteau| Beale and Nunn",109,"George","COD","Standard",,109,8/14/1998 8:50:02
NingZhang.info
宁章资讯
回答by Joel
Here's a bit of Python that seems to do the trick:
下面是一些似乎可以解决问题的 Python:
>>> import re
>>> p = re.compile('["][^"]*["]|[^,]*')
>>> x = """1001,"Fitzsimmons, Des Marteau, Beale and Nunn",109,"George","COD","Standard",,109,8/14/1998 8:50:02"""
>>> y = p.findall(x)
>>> ','.join(z.replace(',','|') for z in y if z)
'1001,"Fitzsimmons| Des Marteau| Beale and Nunn",109,"George","COD","Standard",109,8/14/1998 8:50:02'
Seems like this code turn into a code golf question :-)
似乎这段代码变成了一个代码高尔夫问题:-)
Oops...missed the Java tag.
哎呀...错过了 Java 标签。
回答by Ken Bloom
Well, this is a CSV file, so I'd use Ruby's built-in CSV library. Then you don't have to figure out how to deal with escaped quotation marks, for example.
嗯,这是一个 CSV 文件,所以我会使用 Ruby 的内置 CSV 库。那么你就不必弄清楚如何处理转义引号,例如。
require 'csv'
string =<<CSV
1001,"Fitzsimmons, Des Marteau, Beale and Nunn",109,"George","COD","Standard",,109,8/14/1998 8:50:02
CSV
csv=CSV.parse string
csv.each{|row| row.each {|cell| cell.gsub!(",","|") if cell.is_a?(String)}}
outstring = ""
CSV::Writer.generate(outstring){|out| csv.each {|row| out<<row}}
回答by Steve Wortham
I believe this is going to be very difficult to do with a regular expression. The trouble is that the regular expression would have to count quotes to determine if it's inside two quotes or not.
我相信用正则表达式很难做到这一点。问题是正则表达式必须计算引号以确定它是否在两个引号内。
Actually, the .NET regex engine could do it with its balanced matchingfeature. But I don't think Java has that feature and I can't think of a reliable way to do it without it.
实际上,.NET 正则表达式引擎可以通过其平衡匹配功能来做到这一点。但我不认为 Java 有这个特性,如果没有它,我想不出一个可靠的方法来做到这一点。
You may have to write some procedural code to accomplish this.
您可能需要编写一些过程代码来完成此操作。

