Regex: word boundary but for white space, beginning of line or end of line only

regex

Solution

OK, so your real question is:

How do I match a unit, optionally preceded by a quantity, but only if there is either nothing or a space right before the match?

Use

 (?<!\S)\b(?:\d+\s*x\s*)?\d+(?:\.\d+)?\s*ml\b

Explanation

`(?<!\S)`: Assert that it's impossible to match a non-space character before the match.

`\b`: Match a word boundary

`(?:\d+\s*x\s*)?`: Optionally match a quantifier (integers only)

`\d+(?:\.\d+)?`: Match a number (decimals optional)

`\s*ml\b`: Match `ml`, optionally preceded by whitespace.

Problem

I am looking for some word boundary to cover those 3 cases: - beginning of string - end of string - white space Is there something like that since \b covers also -,/ etc.? Would like to replace \b in this pattern by something described above: ``` (\b\d*\sx\s|\b\d*x|\b) ```

Original source

Related problems