VBA Excel正则表达式-如果单词在字符串的开头,\ b单词边界不匹配 [英] VBA Excel regex - \b word boundary doesn't match if word is at beginning of string

查看:288
本文介绍了VBA Excel正则表达式-如果单词在字符串的开头,\ b单词边界不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在excel中使用VBA正则表达式引擎来匹配以单词边界开头的单词(例如\babc).

I'm using VBA regex engine in excel to match a word that starts on a word boundary (e.g. \babc).

但是,如果单词在字符串的开头,则此方法不起作用.

However, this doesn't work if the word is at the beginning of the string.

一个选择是在字符串的开头(例如^abc)对匹配项进行另一个比较."但是,这会使代码更加复杂.是否有更好的解决方案?

One option would be to do another comparison for a match at the beginning of the string (e.g. ^abc)". But, this makes the code a little more complicated. Is there a better solution?

推荐答案

可以使用交替吗?

(?:\b|^)abc

更新:

至少在Excel 2010中,\b可以正常工作.

In Excel 2010 at least, \b works as expected.

Sub test()
    Set re = CreateObject("VBScript.RegExp")
    re.Pattern = "\babc"
    MsgBox re.test("abc")
    re.Pattern = "^abc"
    MsgBox re.test("abc")
    re.Pattern = "(?:\b|^)abc"
    MsgBox re.test("abc")
End Sub

上面为我输出True,True,True.

The above outputs True, True, True for me.

这篇关于VBA Excel正则表达式-如果单词在字符串的开头,\ b单词边界不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆