VB6正则表达式替换

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

它确实有效,但仅替换一个字符。我如何替换多个字符?例如:“ÉPÉ”应该是“<em>P</em>”,但目前结果是:“_PÉ”?

2个回答

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
Dim strPattern As String: strPattern = "[^a-zA-Z0-9]*" 
Dim regex As New RegExp
regex.Pattern = strPattern
result = regex.Replace(pFileNameWithoutExtension, "_")

@Gordon:这只是匹配一个不在列表中的零个或多个字符的字符串,它会在列表中的第一个字符处终止。 - MyItchyChin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接