1

So I'm doing a little bit of testing for something and I require a method of splitting a string into groups of two. (e.g. 'abcdef' => ['ab','cd','ef'])

I'm trying to use a regex pattern to do this ([^]{2}). Whenever I try to compile this pattern, I get the error message:

sre_constants.error: unexpected end of regular expression

The exact line of code is:
pat = re.compile(r'[^]{2}')

Could someone please tell me what I'm doing wrong here? I've done a lot of searching but a lot of the problems were related to incorrect usage and/or backslashes.

I thought about it possibly being because of string formatting, though the Python docs didn't mention anything about any issues.

Spedwards
  • 4,167
  • 16
  • 49
  • 106

2 Answers2

3

Use

(.{2})

Dot will match any character. If you want to match newline characters with dot do not forget to add s modifier. So your code will look like this

p = re.compile('(?s)(.{2})')

Also I am not sure why you want to use regular expressions for the task. You can do it with following snippet

In [5]: line = 'abcdef'

In [6]: n = 2

In [7]: [line[i:i+n] for i in xrange(0, len(line), n)]
Out[7]: ['ab', 'cd', 'ef']
Konstantin
  • 24,271
  • 5
  • 48
  • 65
0

It is true that in JavaScript [^] means "match any character" (though even regex101 says: "Note: Avoid this construct, use . or [\s\S] instead."). However, in Python, [^] is an invalid character class (see this example on regex101).

Use (?s)(.{2}).

See demo.

(?s) inline option will make sure you also match the newline characters.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • It seems weird to me that `[^]` is invalid in Python. I swear I've used it before and it worked fine. – Spedwards Apr 28 '15 at 11:05
  • Have a look at http://goo.gl/uKQenL, the program is not compiling. Python is looking for some characters inside the character class, but does not find them, so it fails. – Wiktor Stribiżew Apr 28 '15 at 11:11