-2

Why does re.compile(r"^[\s]+|[\s]+$") matches trailing and leading whitespaces in a string " Hello ", but re.compile(r"^[\s]+[\s]+$") does not? The latter (without |) seems perfectly fine to me:

  • ^[\s]+ - should match at least one trailing whitespace at the beginning of the searched string
  • [\s]+$ - should match at least one leading whitespace at the end of the searched string

But why doesn't it work? I don't understand how the bitwise OR (|) operator makes the difference.

  • 3
    `"^[\s]+[\s]+$"` does not match with your string because you have char between – thibsc May 07 '20 at 22:10
  • This is not bitwise or. From the [docs](https://docs.python.org/3/library/re.html#regular-expression-syntax): *`A|B`, where A and B can be arbitrary REs, creates a regular expression that will match either A or B.* – Tomerikoo May 07 '20 at 22:28

2 Answers2

0

I don't understand how the bitwise OR (|) operator makes the difference.

…because it is not a bitwise OR - we are talkin regexp here, it is an alternative. a|b in regexp means "match either a or b". So your first regepx matches either leading or trailing spaces. Your second regexp matches… just spaces. ONLY spaces, from the beginning to the end of the string. To match spaces, some non-spaces, then more spaces you could use: r"^\s+\S+\s+$" (\S means "anything but whitespace characters").

Błotosmętek
  • 11,945
  • 16
  • 26
0
re.compile(r"^[\s]+[\s]+$")

actually means

  • ^ match beginning of a line
  • [\s]+ match at least one whitespace (actually, writing just \s+ would be sufficient)
  • [\s]+ match more whitespace immediately after the previous whitespaces
  • $ until the end of line

The OR expression | basically divides your pattern: it matches either whitespace at the start and/or whitespace at the end.

Your string matches the latter, but not the former, since it doesn't contain only whitespace.

amain
  • 1,438
  • 11
  • 19