I'm trying to find the best approach to replace a specific pattern with a character in python. For example if I have the text "prop1": "val1","prop2": "val2" "abcdefg": "hijklmn" "1234": "5678" But I want the string: "prop1": "val1","prop2": "val2","abcdefg": "hijklmn","1234": "5678" I found this pattern seems to group the space between the sets of quotes correctly from regex101 '"\S*"(\s{1})"\S*"' But when using this in python it seems this is not replacing the group rather the entire match or some other variant behavior. Code: testStr = 'prop1": "val1","prop2": "val2" "abcdefg": "hijklmn" "1234": "5678' testMatch = re.search('"\S*"(\s{1})"\S*"', testStr) print(f'Full match: {testMatch.group(0)}') testGroupMatch = testMatch.group(1) print(f'Group match: {testGroupMatch}') print(f'Test string before replace: {testStr}') testStrReplaced = re.sub('"\S*"(\s{1})"\S*"', ',', testStr) print(f'Test string after replace: {testStrReplaced}') Output: Full match: "val2" "abcdefg" Group match: Test string before replace: prop1": "val1","prop2": "val2" "abcdefg": "hijklmn" "1234": "5678" Test string after replace: prop1": "val1","prop2": ,: ,: "5678" Does anyone know if this is the right approach for this kind of scenario? If so does the regex expression look correct to target the pattern I'm trying to replace? Does anyone know how I would replace the matched group? Most of the examples I've found mention backreferencing the groups, however, this seems to be if I want to replace something with a group I've already matched. In this case I simply want to replace the matched group, which from my test output is just the space, with a single character such as a comma. Thanks! Continue reading...