Home > Enterprise >  Is there a way to capture a specific values between parentheses so they can be replaced
Is there a way to capture a specific values between parentheses so they can be replaced

Time:11-03

I am trying to use REGEX in Python to parse out a document. I want to replace all instances of * that appear within parentheses. eg ("A2OR01" * "A2OR02" * "A2OR03") to ("A2OR01" , "A2OR02" , "A2OR03").

So far I have tried (. (*) . ) but it only will match the last instance of the *. For example when I replace with the REGEX expression that I wrote I get ("A2OR01" * "A2OR02" , "A2OR03"). I need all instances of * that are surrounded by parentheses to be replaced not just the last. I want to ignore all the values between the parentheses as they can vary including whitespaces.

CodePudding user response:

Here's my code:

import re
s = '("A2OR01" * "A2OR02" * "A2OR03")'
res = re.findall('\(.*?\)', s) # Get all instances of strings within parantheses (list)
print(res) # ['("A2OR01" * "A2OR02" * "A2OR03")']
for i in range(len(res)):
    res[i] = res[i].replace("*",",") # Replace all instances of * within parantheses
print(res) # ['("A2OR01" , "A2OR02" , "A2OR03")']

CodePudding user response:

I wouldn't bother with regex for something that's so trivial to implement without additional modules.

def replace_asterisks(s):
    pcount = 0
    chars = []
    for c in s:
        match c:
            case '(':
                pcount  = 1
            case ')':
                pcount -= 1
            case '*':
                if pcount > 0:
                    c = ','
        chars.append(c)
    return ''.join(chars) if pcount == 0 else s

print(replace_asterisks('("A2OR01" * "A2OR02" * "A2OR03")'))
print(replace_asterisks('"A2OR01" * "A2OR02" * "A2OR03")'))
print(replace_asterisks('("A2OR01" * "A2OR02" * "A2OR03"'))

Output:

("A2OR01" , "A2OR02" , "A2OR03")
"A2OR01" * "A2OR02" * "A2OR03")
("A2OR01" * "A2OR02" * "A2OR03"
  • Related