0

WHAT is wrong with the following regular expression and HOW does one fix it?:

    \b\*\b    

The above pattern should be interpreted as word boundary followed by star followed by word boundary.

star, Jeffrey E. F. Friedl's preferred term for the asterisk metacharacter must be escaped by preceding it with a \ backslash when one does not wish to use it as a metacharacter.

For the string * "spacestarspace", the above regular expression fails. Regex Tester for .NET tests the above regular expression.

Here is the same example as above, but with the ignore case option selected.

The following tweak attempts also fail:
\b[\*]\b character class for star
\b(\*)\b constraining parentheses

other combinations that were tried also failed.

Searching with Google, including SO and MSDN, has been unsuccessful.

in a, \ba\b the a is found;
in *, \b\*\b fails to match the star

The same results occur in LINQPad 5 and Visual Studio 2015.

Edit (this fixes the problem for c# .NET) TIMTWOTDI:

\B\*\B

See this in the Regex Tester for .NET.

Wiktor Stribiżew's comment was the clue that helped.

On page 133 of the third English edition of Friedl's book, the author wrote "it would make sense if the word boundaries agree with \w, but that is not always the case".

gerryLowry
  • 2,626
  • 5
  • 35
  • 44
  • 1
    What is the problem? `\b\*\b` matches a string like `1*2`. `*` is not a word char, there is no word boundary between a space and `*`. – Wiktor Stribiżew May 26 '17 at 16:48
  • Do you just want to match any chars that are not preceded/followed with word chars? Use [`@"(?<!\w)\*(?!\w)"`](http://regexstorm.net/tester?p=%28%3f%3c!%5cw%29%5c*%28%3f!%5cw%29&i=+*+&o=i) – Wiktor Stribiżew May 26 '17 at 16:56
  • Neither space nor star are words, so space**star**space will never match. Simply put, a word boundary must be between a word and a non-word character. –  May 26 '17 at 17:10
  • @WiktorStribiżew your first comment helped me see my mistake; `\B\*\B` solves this for me in c# .NET. My purpose was to determine whether a not T-SQL SELECT statement was a `SELECT * ` for Advantage Database Server; to append `rowID`, one must write `TABLE.*, rowID ` because the Advantage SQL Engine does not like ` *, rowID ` ~~ thank you Wiktor – gerryLowry May 26 '17 at 17:30
  • OK, `\B` matches anywhere but at the places matched with a word boundary. – Wiktor Stribiżew May 26 '17 at 17:42
  • @WiktorStribiżew imho my question is not a duplicate because it is specifically about finding an asterisk whereas [Regex word boundary expressions](https://stackoverflow.com/questions/3468102/regex-word-boundary-expressions) is far more *generic*. – gerryLowry May 26 '17 at 17:55
  • Well, your question is the result of misunderstanding what word boundaries match. I think it is a dupe. – Wiktor Stribiżew May 26 '17 at 17:58

1 Answers1

0

You don't provide sample strings against which regex is executed, but the following do match.

a*b
a*b*c*d
asdlfkj*fjldsk
1*2

It seems you're testing <space>*<space> and the reason that doesn't match is because the space is not a word, and neither is the *, so there is no word boundary between them, and hence no match.

LB2
  • 4,802
  • 19
  • 35