in reply to Re^2: regex search for words with one digit
in thread regex search for words with one digit
Why has everybody else kept the \b?
I can't answer for others, but for me, throwing in boundary assertions like this is a reflexive, defensive (and possibly cargo-cultish) move I tend to use when I'm dealing with "words".
The string the Anonymous Monk gives as an example is fairly straightforward: it's delimited by whitespace and the beginning and end of the string. As you say, \b will not help here (update: No! See tybalt89's reply), though it does no harm.
Unfortunately, "words" be tricky. Is "word's" one word or two? If it's supposed to be one word, then Anonymous Monk's /\b\w*\d\w*\b/ or /\w*\d\w*/ or, I think, any of the other solutions I've seen so far will fail to match it with or without \bs. Words like "t'other", "wouldn't've", "words'" or "left-handed" can be difficult to deal with. I'm sure one could give many other examples, and that's just in English!
In general, I think (?<! \S) and (?! \S) would serve better than \b as word boundaries (update: in the OPed case). But once again, it is unfortunately true that there are few generalities in human language.
Give a man a fish: <%-{-{-{-<
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: regex search for words with one digit
by tybalt89 (Monsignor) on Sep 21, 2020 at 23:37 UTC | |
by AnomalousMonk (Archbishop) on Sep 23, 2020 at 04:47 UTC | |
by hippo (Archbishop) on Sep 23, 2020 at 08:14 UTC | |
by Bruder Savigny (Initiate) on Sep 24, 2020 at 00:56 UTC |