如何从 VBscript 中的字符串中删除字符

发布于 2024-12-06 00:05:51 字数 321 浏览 5 评论 0原文

我是 vbscript 和 Stack Overflow 的新手,确实需要一些帮助。

目前,我正在尝试格式化从图像读取并存储在变量中的电话号码。因为图像是“脏”的,所以会出现额外的字符,例如句号或括号。我已经尽可能地限制了该字段,以防止拾取额外的字符,但是唉!

例如我想将“,123.4567890”转换为“123 456 7890”(不包括双引号)。问题是我不知道可能会选取哪些额外的字符,从而排除了简单的替换。

我的逻辑是删除所有非数字字符,从左侧开始,在第三个数字后插入一个空格,在第六个数字后插入一个空格。

任何帮助都会很棒,如果需要,请随时询问更多信息。

I'm new to vbscript and Stack Overflow, and could really use some help.

Currently I'm trying to format a phone number that is read from an image and stored in a variable. Because the images are "dirty" extra characters find their way in, such as periods or parenthesis. I've already restricted the field as much as possible to help prevent picking up extra characters, but alas!

For example I want to turn ",123.4567890" into "123 456 7890" (not including the double quotes). Trouble is I won't know what extra characters could potentially be picked up, ruling out a simple replace.

My logic is remove any non numeric characters, start from the left, insert a space after the third number, insert a space after the sixth number.

Any help would be great, and please feel free to ask for more information if needed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狼亦尘 2024-12-13 00:05:51

欢迎来到堆栈溢出。您可以使用 Regex使用 连接 各部分中功能。

例如:

Dim sTest
sTest = ",123.4567890"

With (New RegExp)
    .Global = True
    .Pattern = "\D" 'matches all non-digits
    sTest = .Replace(sTest, "") 'all non-digits removed
End With

WScript.Echo Mid(sTest, 1, 3) & " "& Mid(sTest, 4, 3) & " "& Mid(sTest, 7, 4)

或完全使用正则表达式(通过第二个分组模式):

Dim sTest
sTest = ",123.4567890"

With (New RegExp)
    .Global = True
    .Pattern = "\D" 'matches all non-digits
    sTest = .Replace(sTest, "") 'all non-digits removed
    .Pattern = "(.{3})(.{3})(.{4})" 'grouping
    sTest = .Replace(sTest, "$1 $2 $3") 'formatted 
End With

WScript.Echo sTest

Welcome to Stack Overflow. You can remove non-digits using Regex, and concatenate the parts using Mid function.

For example:

Dim sTest
sTest = ",123.4567890"

With (New RegExp)
    .Global = True
    .Pattern = "\D" 'matches all non-digits
    sTest = .Replace(sTest, "") 'all non-digits removed
End With

WScript.Echo Mid(sTest, 1, 3) & " "& Mid(sTest, 4, 3) & " "& Mid(sTest, 7, 4)

Or fully using Regex (via a second grouping pattern):

Dim sTest
sTest = ",123.4567890"

With (New RegExp)
    .Global = True
    .Pattern = "\D" 'matches all non-digits
    sTest = .Replace(sTest, "") 'all non-digits removed
    .Pattern = "(.{3})(.{3})(.{4})" 'grouping
    sTest = .Replace(sTest, "$1 $2 $3") 'formatted 
End With

WScript.Echo sTest
牵强ㄟ 2024-12-13 00:05:51

使用第一个正则表达式来清除输入中的非数字,第二个正则表达式使用布局组来清除:

Function cleanPhoneNumber( sSrc )
  Dim reDigit : Set reDigit = New RegExp
  reDigit.Global  = True
  reDigit.Pattern = "\D"
  Dim reStruct : Set reStruct = New RegExp
  reStruct.Pattern = "(.{3})(.{3})(.+)"
  cleanPhoneNumber = reStruct.Replace( reDigit.Replace( sSrc, "" ), "$1 $2 $3" )
End Function ' cleanPhoneNumber

Use a first RegExp to clean non-digits from the input and second one using groups for the layout:

Function cleanPhoneNumber( sSrc )
  Dim reDigit : Set reDigit = New RegExp
  reDigit.Global  = True
  reDigit.Pattern = "\D"
  Dim reStruct : Set reStruct = New RegExp
  reStruct.Pattern = "(.{3})(.{3})(.+)"
  cleanPhoneNumber = reStruct.Replace( reDigit.Replace( sSrc, "" ), "$1 $2 $3" )
End Function ' cleanPhoneNumber
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文