match wordchar and/or dot string of anylength

147 views Asked by At

This matches a word /\w\+
This matches a any number of dots /\.\+

Why doesn't this match any number of words combined by dots? /[\w\.]\+ ?

The w seems to be matching actual 'w's instead of a word character, whether I escape it or not.

1

There are 1 answers

0
Wiktor Stribiżew On BEST ANSWER

See "PREDEFINED RANGES" in Vim documentation: usr_27:

Note:
Using these predefined ranges works a lot faster than the character range it stands for.
These items can not be used inside []. Thus [\d\l] does NOT work to match a digit or lowercase alpha. Use \(\d\|\l\) instead.

So, your work-around would be /[a-zA-Z\.]\+ if you want to exclude digits and underscore that are matched with \w, or /[a-zA-Z0-9_\.]\+ to emulate the \w functionality. If POSIX bracketed classes are supported, /[[:alpha:]\.]\+ (or for full emulation, /[[:alpha:][:digit:]_\.]\+) is also an option.

There are also other ways, see this SO post where 2 other alternatives are suggested:

  • A non-capturing sub-expression\%(\w\|\.\)\+
  • A sequence of optionally matched atoms \%[\w\.]\+