0

Just checking that I've got the right handle on this as it seems odd.

I've got a small python3 script looking for substrings in a file where the data are dropped into various 512 byte blocks. So, it's possible for strings to be split. A naive approach takes the substring and splits it into the various pairwise combinations, using these as patterns for re.findall. However, special characters in regex patterns have to be escaped:

>>> a=b'1$2'
>>> s=b'1231$234'
>>> re.findall (a,s)
[]     # fails as '$' in a is interpreted as regex 'end of line'
>>> c=b'1\$2'
>>> re.findall(c,s)
[b'1$2']

If I want to iterate over the pairs that make up c in the above code, is there no simple way to avoid having to treat the '\' as a special character? It makes sense from the pattern definition language, which using '\' as an escape character means that c[:2] is an illegal pattern ("1\"). But it's the sort of special case that it would be nice to be without.

user676952
  • 33
  • 2

0 Answers0