How to make sure that regex is not in [] or {}?

Viewed 64

I am trying to replace all the word in a give sentence with a random word list. Here is my code:

import re
import random

WORDS = ["Brawk" , "Buh-gok", "Bok bok", "Bawk"] # My random word list
PATTERN = r"([a-zA-Z0-9\']+)"
DELIMITER = " "

def callback(matchobj):
    return random.choice(WORDS)

def parse_sentence(sentence):
    return re.sub(PATTERN, callback, sentence)

When I run the code, this is what happens:

>>> print(parse_sentence("some text's[color=#ff8a00]smoe more text[/color]{n}"))
Bok bok Bok bok[Buh-gok=#Buh-gok]Bok bok Buh-gok Buh-gok[/Bawk]{Brawk}

I need it to be Buh-gok Bok bok[color=#ff8a00]Bok bok Bok bok Bawk[/color]{n}, so is there anyway to ignore it if it is inside [] or {}?

1 Answers

You need to modify two things in the code:

PATTERN = r"(\[[^][]*]|\{[^{}]*})|[a-zA-Z0-9']+"

def callback(matchobj):
    return matchobj.group(1) or random.choice(WORDS)

See the Python demo.

The regex - (\[[^][]*]|\{[^{}]*})|[a-zA-Z0-9']+ - matches and captures into Group 1 all substrings between square brackets and between curly braces and just matches alphanumeric character chunks, and return matchobj.group(1) or random.choice(WORDS) returns either Group 1 values (if Group 1 matched), or the random words if Group 1 did not match.

Pattern details:

  • ( - start of a capturing group #1
    • \[[^][]*] - [, then any zero or more chars other than ] and [, and then a ] char
    • | - or
    • \{[^{}]*} - {, then any zero or more chars other than { and }, and then a } char
  • ) - end of the group
  • | - or
  • [a-zA-Z0-9']+ - one or more ASCII letters or digits or '.
Related