import re with open('src/main/java/com/example/whatsappautomation/service/ChatbotService.java', 'r') as f: content = f.read() # Pattern to match sendInteractiveList(...) up to the null); # We need to capture bodyText (arg 1), ids (arg 4), titles (arg 5). # Using a python script to parse the balanced parentheses might be better. def replace_calls(text): start = 0 while True: idx = text.find('sendInteractiveList(', start) if idx == -1: break # find the end of this statement end_idx = text.find(');', idx) if end_idx == -1: break call_str = text[idx:end_idx+2] # We need to extract the parts. It's safer to just split by "new String[]{" # because we know args 4 and 5 are array allocations. parts = call_str.split('new String[]{') if len(parts) == 3: # parts[0] has 'sendInteractiveList(bodyText, buttonText, sectionTitle, \n' # parts[1] has 'ids...}, \n' # parts[2] has 'titles...}, null);' # Extract bodyText from parts[0] # sendInteractiveList(arg1, arg2, arg3, # We want just arg1! p0 = parts[0][len('sendInteractiveList('):].strip() # It's tricky to parse commas. pass print("Doing regex...") # Actually, let's just use a simple regex if they are formatted consistently. # Almost all of them are: # sendInteractiveList( BODY, BTN, TITLE, # new String[]{IDS}, # new String[]{TITLES}, null); pattern = r'sendInteractiveList\((.*?),\s*[^,]+,\s*[^,]+,\s*(new String\[\]\{.*?\})\s*,\s*(new String\[\]\{.*?\})\s*,\s*null\);' replaced = re.sub(pattern, r'sendPaginatedButtons(state, \1, \2, \3);', content, flags=re.DOTALL) with open('src/main/java/com/example/whatsappautomation/service/ChatbotService.java', 'w') as f: f.write(replaced) print("Done")