Brian Sykes
Brian Sykes

Reputation: 71

Python Regex Pattern Replacement

i'm trying to esssentially get any dynamic input from a python script, in this case its a simple file organizing script. I need to make sure that any instance of [ or ] is wrapped like this [[] []].

So naturally i tried replace but that just put brackets around all of the escape brackets(the escape brackets for using glob.glob)

That proved useless so now i turn to re.sub but i can't seem to find a pattern that will only replace [ or ] with its escape counterpart if the [ or ] has no brackets around it.

I have no idea if that makes sense to anyone but thats pretty much it, here is the messed up re pattern i've got so far, it doesn't like me.

pattern = r'[^\[]([\[])[^\]]'

Upvotes: 0

Views: 112

Answers (1)

Alfe
Alfe

Reputation: 59436

I'd choose a solution using the higher-order feature of re.sub to handle tokens (tokenizing is common practice in parsing computer languages):

def replaceToken(match):
    token = match.group()
    if len(token) == 3:
        return token
    else:
        return '[' + token + ']'

re.sub(r'(\[\[\])|(\[\]\])|\[|\]', replaceToken, 'foo[[bar]bloh')

Or in one call, if you prefer that:

re.sub(r'(\[\[\])|(\[\]\])|\[|\]',
       lambda x: x.group() if len(x.group()) == 3
                           else '[' + x.group() + ']', 'foo[[]bar]bloh')

Results:

'foo[[bar]bloh' → 'foo[[][[]bar[]]bloh'
'foo[[]bar]bloh' → 'foo[[]bar[]]bloh'

Upvotes: 3

Related Questions