且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

正则表达式中的单词边界是什么,\b 是否匹配连字符“-"?

更新时间:2023-02-18 21:35:28

在大多数正则表达式方言中,单词边界是 \w\W 之间的位置(非单词字符),或者在字符串的开头或结尾,如果它以单词字符([0-9A-Za-z_])开头或结尾(分别).

A word boundary, in most regex dialects, is a position between \w and \W (non-word char), or at the beginning or end of a string if it begins or ends (respectively) with a word character ([0-9A-Za-z_]).

因此,在字符串 "-12" 中,它将匹配 1 之前或 2 之后.破折号不是单词字符.

So, in the string "-12", it would match before the 1 or after the 2. The dash is not a word character.