Hi There,
This seems like a FAQ, but I haven't been able to find the answer after some looking. Textmate doesn't have a "match entire word" check box in the find dialog, like most text editors. Presumably, we're supposed to use regular expressions to achieve the same effect. Most times, the word is a variable name. So for my variable x in C++, I want to find all uses including:
x[i] x->foo x.bar
I've been trying to bracket the variable with the "non word character", so I search for the regular expression \Wx\W
This excludes xbar, and foox. However, this isn't quite the same as matching just the word, as the find dialog also selects the previous and next character, matching the \W. I can't use it in a replace, for example. Is there some cleaner way of doing this?
Thanks, Craig Schmidt
You probably want to use \bx\b which will only match at word boundarys.
On Jul 18, 2007, at 12:37 PM, Craig Schmidt wrote:
I've been trying to bracket the variable with the "non word character", so I search for the regular expression \Wx\W
This excludes xbar, and foox. However, this isn't quite the same as matching just the word, as the find dialog also selects the previous and next character, matching the \W. I can't use it in a replace, for example. Is there some cleaner way of doing this?
You need to employ a zero-width assertion/anchor. This is a way of anchoring/matching w/o including the match itself. Examples are (from the Oniguruma help):
^ beginning of the line $ end of the line \b word boundary \B not word boundary \A beginning of string \Z end of string, or before newline at the end \z end of string
There is also a general mechanism:
(?=subexp) look-ahead (?!subexp) negative look-ahead (?<=subexp) look-behind (?<!subexp) negative look-behind
The perl regular expression engine is sufficiently similar to Oniguruma that you may find the "perlre" man page helpful. I also highly recommend Mastering Regular Expressions.
To answer your specific question, you probably want \bx\b
j.
I'm not totally following you, but it sounds like you want to search over the pattern boundaries without matching them. If so, then you want to look at the look-ahead, (?=foo), and look-behind (?<=foo) expressions.
HTH, -- Ian
On Jul 18, 2007, at 12:34 PM, Craig Schmidt wrote:
Hi There,
This seems like a FAQ, but I haven't been able to find the answer after some looking. Textmate doesn't have a "match entire word" check box in the find dialog, like most text editors. Presumably, we're supposed to use regular expressions to achieve the same effect. Most times, the word is a variable name. So for my variable x in C++, I want to find all uses including:
x[i] x->foo x.bar
I've been trying to bracket the variable with the "non word character", so I search for the regular expression \Wx\W
This excludes xbar, and foox. However, this isn't quite the same as matching just the word, as the find dialog also selects the previous and next character, matching the \W. I can't use it in a replace, for example. Is there some cleaner way of doing this?
Thanks, Craig Schmidt
For new threads USE THIS: textmate@lists.macromates.com (threading gets destroyed and the universe will collapse if you don't) http://lists.macromates.com/mailman/listinfo/textmate