1

From my textbook (automate the boring stuff, Al Sweigart): "If you want a regular expression that's case-insensitive and includes newlines to match the dot character, you would form your re.compile() call like this:

someRegexValue = re.compile('foo', re.IGNORECASE | re.DOTALL)

Unfortunately, the re.compile() function takes only a single value as its second argument. You can get around this limitation by combining the re.IGNORECASE and re.DOTALL variables using the pipe character (|), which in this context is known as the bitwise or operator.

This page is given as additional resource: https://wiki.python.org/moin/BitwiseOperators/

I don't understand why we don't use the bitwise and operator instead. Referring to that page, if we regard x and y as some matching conditions and both must be applied at the same time, I would intuitively use the and operator...

Is there a reason why we can't do that?

Nick
  • 138,499
  • 22
  • 57
  • 95
aurumpurum
  • 932
  • 3
  • 11
  • 27
  • A bit-wise and would always result in `0` for any combination of fields in the bitfield (except for the same). – Klaus D. Jan 03 '21 at 11:24
  • 1
    Does this answer your question? [What are bitwise operators?](https://stackoverflow.com/questions/276706/what-are-bitwise-operators) – Ryszard Czech Jan 03 '21 at 21:10
  • @RyszardCzech the post covers topics that are not familiar to me, so I am still having problems to understand the concept. – aurumpurum Jan 04 '21 at 21:03

1 Answers1

4

The various re.FLAGS are part of a bit mask, with each flag being some value of 2 to occupy some bit position. For example:

re.IGNORECASE =  2 = 00010
re.DOTALL     = 16 = 10000

Therefore, the expression re.IGNORECASE | re.DOTALL would generate the following bit mask:

10010

The flags parameter to many of the re library functions are expecting such a bitmask to decide which flags to apply. If we were to bitwise & the above two operators, we would just get zero. In fact, we would get zero for any bitwise & between two different flags, since no bits would ever be overlapping.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Ok, thanks, but it's still hard to understand. Maybe because I am not really familiar with bitwise operators, flags, bit masks and all that stuff you mentioned. Coming from if conditions in python and combining multiple conditions with and / or operators, I have to admit, this works differently with bitwise operators... – aurumpurum Jan 03 '21 at 11:36
  • Maybe review the basics of bitwise `&` and `|`. – Tim Biegeleisen Jan 03 '21 at 11:39
  • Ok, I have found this post, which also might be helpful. But I have to do some additional research on that topic: https://stackoverflow.com/questions/276706/what-are-bitwise-operators?rq=1. Thanks. – aurumpurum Jan 03 '21 at 11:46
  • @aurumpurum bit-level flagging is a really common approach in a lot of systems that need to track multiple on/off flags. Not just regexes. You might for example see **required visible editable ....** tracked in a database column storing GUI field data. – JL Peyret Jan 03 '21 at 17:23
  • "Ok, I have found this post, which also might be helpful"... but... that's the same one that you later said didn't answer your question (it does)? – Karl Knechtel Oct 16 '22 at 03:26