[Tutor] Regex ^$ not behaving as expected

Edmund Butterworth edmundb at talktalk.net
Thu Dec 8 13:53:53 EST 2016


Dear Danny,

That was great, just the information I wanted and very prompt too. I've 
tried the /f//lags  =/ and it works. Thank you so much.

I had been banging my head against this for the last two days, googling 
the universe and even considering going back to PERL, but for a lot of 
reasons I want to do this in Python. I can and shall now.

Best regards

Ed

-------- Original Message --------
*Subject: *Re: [Tutor] Regex ^$ not behaving as expected
*From: *Danny Yoo <dyoo at hashcollision.org>
*To: *Edmund Butterworth <edmundb at talktalk.net>
*Cc: *Python Tutor Mailing List <tutor at python.org>
*Date: *08/12/2016, 17:20:19
> Following up: drats!  Detecting this conceptual TypeError is not
> feasible under the current design, due to the choice of data
> representation used in this API.
>
> The reason is because the flags are being represented as integers, and
> we're using bitwise operations to define the union of flags.  That is:
>
> #######################################
>>>> import re
>>>> re.MULTILINE
> 8
>>>> re.DOTALL
> 16
>>>> re.MULTILINE | re.DOTALL
> 24
> ########################################
>
> Flags are integers here.  Since re.sub is taking two optional
> integer-based arguments, count and flags, Python's type system cannot
> determine that we've passed flags in the wrong place, because it
> legitimately looks like a valid count too.  Bits are bits.
>
> This is a very rough design edge and quite unfortunate.  In an ideal
> world, I can imagine that the representation of flags would be
> different such that this mistake could be caught earlier.  But in
> absence of this, we've just got to be careful, I suppose.  :(
>
>
> Anyway, hope this helps!
>



More information about the Tutor mailing list