Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match at whitespace with at most one newline in regex

Tags:

python

regex

I would like to match a b if between a and b is only whitespace with at most one newline.

Python example:

import re

r = "a\s*b"  # ?

# should match:
print(re.match(r, "ab"))
print(re.match(r, "a   b"))
print(re.match(r, "a \n b"))

# shouldn't match:
print(re.match(r, "a\n\nb"))
print(re.match(r, "a    \n\n  b"))
like image 586
Nico Schlömer Avatar asked Sep 06 '25 03:09

Nico Schlömer


1 Answers

You need to exclude a newline from \s and then optional match a newline with any zero or more whitespace chars other than a newline:

a[^\S\n]*(?:\n[^\S\n]*)?b

See the regex demo.

Details:

  • a - an a letter
  • [^\S\n]* - zero or more whitespace chars other than newline
  • (?:\n[^\S\n]*)? - one or zero occurrences of
    • \n - a newline char
    • [^\S\n]* - zero or more whitespace chars other than newline
  • b - a b letter.
like image 103
Wiktor Stribiżew Avatar answered Sep 07 '25 20:09

Wiktor Stribiżew