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

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

  • beginning of line
  • end of line
  • empty space

Is there something similar since \ b also covers -, / etc.?

I would like to replace \ b in this template with something described above:

(\b\d*\sx\s|\b\d*x|\b)
+3
source share
3 answers

OK, so your real question is:

How can I match an element, optionally preceding the quantity, but only if there is nothing left or a space before the match?

Using

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

Explanation

(?<!\S): Claim that it is not possible to match a non-spatial character before a match.

\b: match the word boundary

(?:\d+\s*x\s*)?: optionally a quantifier (integers only)

\d+(?:\.\d+)?: ( )

\s*ml\b: ml, .

+8

\b (?:^|\s|$)

(
  ?: don't consider this group a match
  ^   match beginning of line
  |   or
  \s  match whitespace
  |   or
  $   match end of line
)

Python JavaScript.

+16

, \b, . , \w\W \w\W. . , , , .

+1
source

Source: https://habr.com/ru/post/1771545/


All Articles