4

I have a regex

(obligor_id): (\d+);(obligor_id): (\d+):

A sample match like below:

Match 1
Full match  57-95   `obligor_id: 505732;obligor_id: 505732:`
Group 1.    57-67   `obligor_id`
Group 2.    69-75   `505732`
Group 3.    76-86   `obligor_id`
Group 4.    88-94   `505732`

I am trying to partially replace the full match to the following:

obligor_id: 505732;obligor_id: 505732: -> obligor_id: 505732;

Two ways to achieve so,

  1. replace group 3 and 4 with empty string

  2. replace group 1 and 2 with empty string, and then replace group 4 to (\d+);

How can I achieve these 2 in python? I know there is a re.sub function, but I only know how to replace the whole, not partially replace group.

Thanks in advance.

2
  • 1
    Why (obligor_id) needs to be a group? Do you only replace repeating ids? If so, consider replacing the entire line that matches obligor_id: (\d+);obligor_id: \1 with "obligor_id: " + match.group(1). Commented Nov 27, 2018 at 22:21
  • 2
    Use re.sub with your pattern and r'\1: \2;' replacement. See the regex demo Commented Nov 27, 2018 at 22:22

2 Answers 2

2

You can change capturing groups and reference them in the substitution string:

s = 'obligor_id: 505732;obligor_id: 505732:' 
re.sub(r'(obligor_id: \d+;)(obligor_id: \d+:)', r'\1', s)
# => 'obligor_id: 505732;
Sign up to request clarification or add additional context in comments.

1 Comment

For using number variables in replacement strings, named reference syntax can be used: re.sub(pat, r'\g<1>' + f'{my_number}', str)
1

Thanks for answers and advices:

I achieved them as below for future users:

re.sub(regex, r'\1: \2;', str)
re.sub(regex, r'\3: \4;', str)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.