如何删除字符? [英] How do I remove the characters?
问题描述
如何删除字符串中的特殊字符和字母?
qwert1234 * 90)! 这可能是我的单元格值
我必须将其转换为
123490我的意思是我必须删除所有内容,但只保留字符串中的数字
但它应该允许空格!
qwe123 4567 *。 90'字符串空格
123 4567 90'输出应为
我发现 vba 替换
- 但是为每个字符写一个替换,使我的代码很大。好吧,让我清楚地告诉你,不要隐瞒你的任何东西:
- 输入:
qwe123 4567 *。 90
'具有空格单元格的字符串(1,A)。值 - 下面我做的这个想法:
123 4567 90
'删除字符保持空格 - 最终输出
A1:A3
123
4567
90
(对于每个空格,它应该插入行并填写)
你能告诉我如何删除除字符串中的数字和空格?
提前感谢
你需要使用正则表达式。
看到这个例子:
Option Explicit
Sub Test()
Const strTest As String =qwerty123 456 uiops
MsgBox RE6(strTest)
End Sub
函数RE6(strData As String)As String
Dim RE As Object,Rematches As Object
Set RE = CreateObject(vbscript.regexp)
使用RE
.MultiLine = False
.Global = True
.IgnoreCase = True
.Pattern =([0-9] | )+
结束
设置REMatches = RE.Execute(strData)
RE6 = Rematches(0)
结束函数
说明:
Pattern = ([0-9] |)+
将匹配包含数字()的任何0个或更多个组(
)或( +
) [0-9] |
)一个空格(
)。 p>
regexp上的一些更多信息:
- < a href =http://www.ozgrid.com/forum/showthread.php?t=37624&page=1 =nofollow noreferrer> ozgrid上的一个线程
- 关于regexp的很好的参考
How do I remove special characters and alphabets in a string ?
qwert1234*90)! ' this might be my cell value
I have to convert it to
123490 ' I mean I have to remove everything but keep only the numbers in string
but it should allow spaces !
qwe123 4567*. 90 ' String with spaces
123 4567 90 ' output should be
I found the vba Replace
- but writing a replace for each character makes my code big. Well let me tell you clearly without hiding anything from you:
- input:
qwe123 4567*. 90
' String with spaces cells(1,"A").value - My idea to do these next:
123 4567 90
' remove characters first keeping white spaces - final output in
A1:A3
123
4567
90
(for every space it should insert row and fill that)
Could you tell me how do remove all characters except numbers and spaces in string?
Thanks In advance
You need to use a regular expression.
See this example:
Option Explicit
Sub Test()
Const strTest As String = "qwerty123 456 uiops"
MsgBox RE6(strTest)
End Sub
Function RE6(strData As String) As String
Dim RE As Object, REMatches As Object
Set RE = CreateObject("vbscript.regexp")
With RE
.MultiLine = False
.Global = True
.IgnoreCase = True
.Pattern = "([0-9]| )+"
End With
Set REMatches = RE.Execute(strData)
RE6 = REMatches(0)
End Function
Explanation:
Pattern = "([0-9]| )+"
will match any 0 or more group (+
) containing a number ([0-9]
) or (|
) a space ().
Some more info on the regexp:
这篇关于如何删除字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!