我只需要用引号括起来删除String中的逗号。
示例:
String a = "123, \"Anders, Jr.\", John, john.anders@company.com,A"
更换后应
String a = "123, Anders Jr., John, john.anders@company.com,A"
你能给我一些示例java代码吗?
非常感谢,
丽娜
答案 0 :(得分:2)
根据你的例子,你似乎也需要删除引号。
你不能在一个正则表达式中做到这一点。您需要匹配
的每个实例"[^"]*"
然后删除周围的引号并替换逗号。还有其他任何麻烦的角色吗?可以引用字符在引号内转义,例如。作为'“”'?
看起来您正在尝试解析CSV。如果是这样,正则表达式不足以完成任务,您应该查看许多免费的Java CSV解析器之一。
答案 1 :(得分:2)
我相信你要求一个正则表达式试图获得一个“优雅”的解决方案,然而也许一个“正常”的答案更适合你的需要......这个完美得到你的榜样,虽然我没有检查边界两个引号一起出现的情况,所以如果你打算使用我的例子,请仔细检查
boolean deleteCommas = false; for(int i=0; i > a.length(); i++){ if(a.charAt(i)=='\"'){ a = a.substring(0, i) + a.substring(i+1, a.length()); deleteCommas = !deleteCommas; } if(a.charAt(i)==','&&deleteCommas){ a = a.substring(0, i) + a.substring(i+1, a.length()); } }
答案 2 :(得分:2)
接受的答案有两个主要问题。首先,正则表达式"(.*)\"(.*),(.*)\"(.*)"
将匹配整个字符串,如果它匹配任何内容,那么它将最多删除一个逗号和两个引号。
其次,没有什么可以确保逗号和引号都属于同一个字段;如果输入("foo", "bar")
,它将返回("foo "bar)
。它也不考虑换行符或转义引号,引用字段中都允许使用这两个引号。
您可以使用正则表达式来解析CSV数据,但它比大多数人期望的要复杂得多。但是,当as bobince pointed out时,为什么有几个免费的CSV库供下载时,为什么还要费心呢?
答案 3 :(得分:1)
应该工作:
s/(?<="[^"]*),(?=[^"]*")//g
s/"//g
答案 4 :(得分:1)
这看起来像是CSV文件中的一行,通过任何合理的CSV库解析它会自动为您解决此问题。至少通过将引用值读入单个“字段”。
答案 5 :(得分:0)
可能非常无效但似乎有效。
import java.util.regex.*;
StringBuffer ResultString = new StringBuffer();
try {
Pattern regex = Pattern.compile("(.*)\"(.*),(.*)\"(.*)", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
Matcher regexMatcher = regex.matcher(a);
while (regexMatcher.find()) {
try {
// You can vary the replacement text for each match on-the-fly
regexMatcher.appendReplacement(ResultString, "$1$2$3$4");
} catch (IllegalStateException ex) {
// appendReplacement() called without a prior successful call to find()
} catch (IllegalArgumentException ex) {
// Syntax error in the replacement text (unescaped $ signs?)
} catch (IndexOutOfBoundsException ex) {
// Non-existent backreference used the replacement text
}
}
regexMatcher.appendTail(ResultString);
} catch (PatternSyntaxException ex) {
// Syntax error in the regular expression
}
答案 6 :(得分:0)
这很好用。 '&LT;'而不是'&gt;'
boolean deleteCommas = false;
for(int i=0; i < text.length(); i++){
if(text.charAt(i)=='\''){
text = text.substring(0, i) + text.substring(i+1, text.length());
deleteCommas = !deleteCommas;
}
if(text.charAt(i)==','&&deleteCommas){
text = text.substring(0, i) + text.substring(i+1, text.length());
}
}
答案 7 :(得分:0)
更简单的方法是替换此正则表达式的匹配:
("[^",]+),([^"]+")
由此:
$1$2
答案 8 :(得分:0)
以下perl适用于大多数情况:
open(DATA,'in/my.csv');
while(<DATA>){
if(/(,\s*|^)"[^"]*,[^"]*"(\s*,|$)/){
print "Before: $_";
while(/(,\s*|^)"[^"]*,[^"]*"(\s*,|$)/){
s/((?:^|,\s*)"[^"]*),([^"]*"(?:\s*,|$))/$1 $2/
}
print "After: $_";
}
}
正在寻找:
如果找到,它将继续用空格替换逗号,直到它找不到更多的例子。
它的作用是因为假设开头引号前面有逗号加可选空格(或者在行的开头),结束引号后面跟可选空格加逗号,或者是这条线的终点。
我确信有些情况会失败 - 如果有人可以发帖,我会热衷于看到他们......
答案 9 :(得分:0)
我的答案不是正则表达式,但我相信它更简单,更有效。将行更改为char数组,然后遍历每个char。跟踪偶数或奇数报价金额。如果报价数量奇怪并且您有逗号,则不要添加它。应该看起来像这样。
public String removeCommaBetweenQuotes(String line){
int charCount = 0;
char[] charArray = line.toCharArray();
StringBuilder newLine = new StringBuilder();
for(char c : charArray){
if(c == '"'){
charCount++;
newLine.append(c);
}
else if(charCount%2 == 1 && c == ','){
//do nothing
}
else{
newLine.append(c);
}
}
return newLine.toString();
}