VBScript正则表达式在数字用引号括起时替换

时间:2017-09-28 18:49:15

标签: regex vbscript

在VBScript中使用Regex我需要用带点的引号chr(34)替换小数点开头的逗号

Input
20170927,Ford,,"6025,00",,"0,00",1,T
20170928,"Fiat, Opel",,"13587,17","13587,17",2,N

Output
20170927,Ford,,"6025.00",,"0.00",1,T
20170928,"Fiat, Opel",,"13587.17","13587.17",2,N

我已尝试过以下代码,但无效。

Dim oRE,text
Set oRE = New RegExp
oRE.Global = True
oRE.Pattern = chr(34)+[0-9],[0-9]+chr(34)
oRE.Replace(text, Replace(text,",","."))

非常感谢。

2 个答案:

答案 0 :(得分:3)

部分问题

在你的正则表达式

oRE.Pattern = chr(34)+[0-9],[0-9]+chr(34)
                     ^

enter image description here

+指示正则表达式引擎需要一个或多个引号后的chr(34)符号

我认为应该在字符集之后移动+符号。

oRE.Pattern = chr(34)[0-9]+,[0-9]+chr(34)
                          ^

提议的解决方案

您应该将打开/关闭引用内部数字捕获到捕获组中,然后在替换中使用。

正则表达式("[0-9]+),([0-9]+")

替换为:$1.$2

请参阅此Live Demo

示例输入文字:

20170927,Ford,,"6025,00",,"0,00",1,T
20170928,"Fiat, Opel",,"13587,17","13587,17",2,N

更换后的样品

20170927,Ford,,"6025.00",,"0.00",1,T
20170928,"Fiat, Opel",,"13587.17","13587.17",2,N

示例VB代码

请注意,内部双引号加倍,以便正确构造字符串。

VB脚本

strTest = "20170927,Ford,,""6025.00"",,""0.00"",1,T" & vbCrLf & "20170928,""Fiat, Opel"",,""13587,17"",""13587,17"",2,N"    
Set re = CreateObject("VBScript.RegExp")
re.Global = True
re.Pattern = "(""\d*),(\d+"")"

strTest = re.Replace(strTest, "$1.$2")

<强> VB.net

Dim sourcestring as String = "replace with your source string"
Dim replacementstring as String = "$1.$2"
Dim matchpattern as String = "(""[0-9]+),([0-9]+"")"
Console.Writeline(regex.Replace(sourcestring,matchpattern,replacementstring))

正则表达式解释

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    "                        '"'
--------------------------------------------------------------------------------
    [0-9]+                   any character of: '0' to '9' (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  ,                        ','
--------------------------------------------------------------------------------
  (                        group and capture to \2:
--------------------------------------------------------------------------------
    [0-9]+                   any character of: '0' to '9' (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    "                        '"'
--------------------------------------------------------------------------------
  )                        end of \2

答案 1 :(得分:3)

请尝试以下代码:

<强>代码:

strTest = "20170928,""Fiat, Opel"",,""13587,17"",""13587,17"",2,N"    'Store the text to be tested in this variable
Set re = New RegExp
re.Global=True
re.Pattern = "(""\d*),(\d+"")"
Set objMatches = re.Execute(strTest)

For Each match In objMatches
    strTest = Replace(strTest,match.Value,match.Submatches.Item(0)&"."&match.Submatches.Item(1))
Next

MsgBox strTest

<强>输出:

enter image description here

使用正则表达式:

("\d*),(\d+")

Click here for Regex Demo

正则表达式说明:

  • ("\d*) - 匹配"后跟0位数的匹配项。括号在第1组中捕获整个部分。
  • , - 按字面意思匹配,
  • (\d+") - 匹配1位出现的数字后跟"。括号在第2组中捕获整个部分。