2009-08-07 22 views
2
Dim strPattern As String: strPattern = "[^a-zA-Z0-9]" 
Dim regex As New RegExp 
regex.Pattern = strPattern 
result = regex.Replace(pFileNameWithoutExtension, "_") 

它可以工作,但它只替換1個字符。我怎樣才能替換多個字符。例如:「ÉPÉ」應該是「P」,但目前的結果是:「_PÉ」?VB6正則表達式替換

回答

9

你只需要啓用全局模式匹配。

Dim strPattern As String: strPattern = "[^a-zA-Z0-9]" 
Dim regex As New RegExp 

regex.Global = True 

regex.Pattern = strPattern 
result = regex.Replace(pFileNameWithoutExtension, "_") 
+0

+1並被接受。不知道這個全球財產。 – 2009-08-07 15:24:59

0
Dim strPattern As String: strPattern = "[^a-zA-Z0-9]*" 
Dim regex As New RegExp 
regex.Pattern = strPattern 
result = regex.Replace(pFileNameWithoutExtension, "_") 
+0

在VB6中不起作用。它返回#PÉ而不是#P#。 – 2009-08-07 15:22:19

+0

@Gordon:它只是匹配不在列表中的0個或更多字符的字符串,它會在列表中的第一個字符處終止。 – MyItchyChin 2009-08-07 15:25:23