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"))
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 newlineb
- a b
letter.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With