tags:

views:

42

answers:

1

I am looking for some word boundary to cover those 3 cases:

  1. beginning of string
  2. end of string
  3. 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)
+2  A: 

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.

Tim Pietzcker
is there an easier way ?
Marcin
Maybe if you could explain what you really want to do. What problem are you trying to solve?
Tim Pietzcker
I have posted my pattern above, just wanted to replace \b by this limited word boundry if possible
Marcin
This is not what "explaining what problem you're trying to solve" means. What do you need the regex *for*? Show some examples of what you're trying to match, what you're trying *not* to match, and possibly any further "rules" the matches have to follow.
Tim Pietzcker
looks nice but not matching floats i.e. 1.50 ml
Marcin
OK, changed the regex to accomodate that. You should know and state your requirements clearly - you hadn't said anything about floats before...
Tim Pietzcker