在两个双引号之间替换逗号

时间:2015-12-16 23:29:58

标签: vb.net

我想在两个双引号之间替换逗号。

实施例

替换:

11/18/2015,15:27,1,103,3,,,197179,"Petco, Inc.",Amy,Jr,187.061,452.5,0,0,0,2.419,0,0,37.38,489.88`

使用:

11/18/2015,15:27,1,103,3,,,197179,"Petco Inc.",Amy,Jr,187.061,452.5,0,0,0,2.419,0,0,37.38,489.88

注意:我仍然想保留简单的逗号,我只想替换双引号内的任何逗号“

我知道我可以通过这样做来替换逗号:strText = Replace(strText, ",", "")

但是我如何仅在两个双引号之间执行此操作,而不会影响双引号之外的其他逗号。

感谢pee2pee,但收到错误:预期的标识符

.Pattern    = "/(".*?"|[^",\s]+)(?=\s*,|\s*$)/g"
-------------------^


  dim re
  Set re = New RegExp
  With re
      .Pattern    = "/(""".*?"""|[^""",\s]+)(?=\s*,|\s*$)/g"
      .IgnoreCase = False
      .Global     = False
  End With
  Set re = Nothing

由于

4 个答案:

答案 0 :(得分:2)

1 - 经典之一:

csv = "..." ' your lines
ReDim chars(Len(csv) - 1) 'array for output
wearein = False
For i = 1 To Len(csv)
    chars(i-1) = Mid(csv, i, 1)
    Select Case chars(i-1)
        Case Chr(34) 'we're in
            wearein = Not wearein
        Case ","
            If wearein Then chars(i-1) = ""
    End Select
Next
newstr = Join(chars, "")
Response.Write newstr

2 - 使用RegExp和回调函数:

Function ReplaceCallback(match, position, all)
    ReplaceCallback = Replace(match, ",", "")
End Function

Set re = New RegExp
    re.Pattern = """[^""]*,[^""]*"""
    re.Global = True

csv = "..." 'your lines
newstr = re.Replace(csv, GetRef("ReplaceCallback")) 'replace done
Response.Write newstr

3 - 通过混合MS JScript和VBScript:

<script language="JScript" runat="server">
function removeCommas(text){
    return text.replace(/"[^"]*,[^"]*"/g, function(){return arguments[0].replace(/,/g, "")});
}
</script>
<%
csv = "..." 'your lines
Response.Write removeCommas(csv)
%>

答案 1 :(得分:1)

您需要的正则表达式模式类似于

/(".*?"|[^",\s]+)(?=\s*,|\s*$)/g

答案 2 :(得分:0)

仅限在这种情况下,您可以使用:

strText = Replace(strText, ", ", " ")

因为你需要替换逗号后只有一个空格。

答案 3 :(得分:0)

一个简单(但可能不那么高效)的解决方案可能是一次更改逗号,如果你传递了不均匀的双引号,在最后一个双引号之后完成。

string origin = ""; //Your string to convert
int lastIndex = origin.LastIndexOf('"');
bool betweenQuotes = false;

for(int i = 0; i < lastIndex; i++)
{
  if(origin[i] == '"')
    betweenQuotes = !betweenQuotes;

  if(origin[i] == ',' && betweenQuotes)
    origin[i] = ' ';
}

编辑:你不能像我在最后一行中那样设置一个字符

origin[i] = ' '; 

它不适用于c#。 把这行改为

origin = origin.Substring(0, i) + " " + origin.Substring(i + 1);

那个解决方案是针对C#的,所以我在VB中创建了一个代码,它做了同样的事情,因为我理解在线ASP工作(或者至少可以工作)用VB。 (抱歉,这是我第一次使用经典的asp和VB)。但VB中的这段代码对我有用。

Dim origin As String = "11/18/2015,15:27,1,103,3,,,197179,fPetco, Inc.f,Amy,Jr,187.061,452.5,0,0,0,2.419,0,0,37.38,489.88"
Dim lastIndex As Integer = 0
Dim betweenQuotes As Boolean = False


For i As Integer = 0 To origin.Length - 1
    If origin(i) = "f" Then
        lastIndex = i
    End If
Next

For i As Integer = 0 To lastIndex
    If origin(i) = "f" Then
        betweenQuotes = Not betweenQuotes
    End If

    If origin(i) = "," AndAlso betweenQuotes Then
        origin = origin.Substring(0, i) + " " + origin.Substring(i + 1)
    End If
Next

我刚用f char替换了双引号,因为我不知道如何在VB中的字符串中放置特殊字符。但是这段代码只替换了F之间的逗号,所以应该没问题。

我还制作了一个JS版本,因为它可以在网络上的任何地方使用,无论你用什么语言创建页面。

var origin = "";
         var lastIndex = 0;
         var betweenQuotes = false;

         for (var i = 0; i < origin.length ; i++)
             if (origin[i] == '"')
                 lastIndex = i;

         for(i = 0; i < lastIndex; i++)
         {
             if (origin[i] == '"')
                 betweenQuotes = !betweenQuotes;

             if (origin[i] == ',' && betweenQuotes) {
                 origin = origin.substr(0, i) + " " + origin.substr(i + 1, origin.length - i - 1);
             }
         }