开发者

Regex to match multiple whole words

How do I write a regex, that works in wxRegEx, to match something this pseudo regex: *good|better|best* ?开发者_StackOverflow中文版

I know about regex matching at the character level, i.e. *.[ch]pp, but how does it go as far as whole words go?

Thanks.


This question is a good example of why you should always specify the flavor of regex you're using. Most regex flavors provide a way to match word boundaries, and most of those use \b for that. According to this page, wxRegEx lets you choose one of three flavors: BRE, ERE, or ARE. Of those, only ARE supports word boundaries, and it uses \y, not \b. Assuming you specify wxRE_ADVANCED when you compile it, this regex will match any string that contains one of the words good, better, or best:

\y(good|better|best)\y

Judging by the examples you used, I think you may be confusing regexes with globs. For example, *.[ch] is the glob you'd use to match file names ending with .c or .h; the regex for that would be \.[ch]$. There's a good regex tutorial at the site I linked to earlier, but the difference I'm most concerned with is that a regex doesn't have to describe the whole string like a glob does. That's why I didn't have to start the regex with .*, but I did have to add $ to the end, to keep it from matching strings like foo.cs or bar.html.


good|better|best

I don't know what you mean by "elegant". As far as I'm concerned, that's pretty elegant.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜