Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

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

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 {}?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 '.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...