Random Facades
Random Facades

Reputation: 23

Python regex ordering issue

I have been running into a problem with python's re module.

This is the simplest version of the issue

import re

print re.findall('a|ab','aab')      # ['a', 'a']
print re.findall('ab|a','aab')      # ['a', 'ab']

I generate regexes at runtime from a text file and cannot be certain that they will be in the correct order. Is there any way around this issue?

Upvotes: 1

Views: 71

Answers (1)

Joran Beasley
Joran Beasley

Reputation: 113940

sort them by size... longer first

eg:

s_regs = sorted(regexes,key=lambda x:len(x))
s_regs.reverse()
regex = '|'.join(s_regs)

Upvotes: 2

Related Questions