r/regex • u/Beautiful-Log5632 • Aug 31 '26
Include underscore in ripgrep word boundary
\bWORD\b doesn't match _WORD_ because \b doesn't include _ so I miss a lot of matches. Is there something else I can use like \b that works like that?
1
u/cscottnet Aug 31 '26 edited Sep 01 '26
(\b_?)WORD(_?\b)
1
u/mfb- Sep 01 '26
Put things in ` to avoid reddit interpreting it as its own formatting.
(\b_?)WORD(_?\b)The brackets don't really do anything here.
\b_?WORD_?\bworks as well. Or(\b|_)WORD(\b|_)depending on what should happen withSTUFF_WORD_STUFF2
u/cscottnet Sep 01 '26
Or
\b_*WORD_*\b-- depends on exactly what OP was trying to achieve. You can use lookahead/lookbehind if they didn't want the underscores to appear in$0.
1
3
u/michaelpaoli Sep 01 '26
\b doesn't match any characters, but rather boundary condition, between word and non-word characters (and also \A ^ $ \z).
So, what exactly are you wanting to match?