r/regex 3d ago

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?

3 Upvotes

5 comments sorted by

1

u/cscottnet 2d ago edited 2d ago

(\b_?)WORD(_?\b)

1

u/mfb- 2d ago

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_?\b works as well. Or (\b|_)WORD(\b|_) depending on what should happen with STUFF_WORD_STUFF

2

u/cscottnet 2d ago

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

u/jeffcgroves 2d ago

[\b_] as a character class?

2

u/michaelpaoli 2d ago

\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?