0

I have following regex that matches any number in the string and returns it in the group.

^.*[^0-9]([0-9]+).*$  $1

Is there a way I can get the text before and after of the matched group i.e. also as my endgoal is to reconstruct the string by replacing the value of only the matched group.

For e.g. in case of this string /this_text_appears_before/73914774/this_text_appears_after, i want to do something like $before_text[replaced_text]$after_text to generate a final result of /this_text_appears_before/[replaced_text]/this_text_appears_after

Maven
  • 14,587
  • 42
  • 113
  • 174
  • 2
    Just use other capturing groups as you had in your previous question... `^(.*[^0-9])([0-9]+)(.*)$` and replace with `$1$3` but note that will only capture one set of digits... – Nick Oct 03 '22 at 03:45
  • @Maven: May be you can add few more examples to clarify your requirement? – anubhava Oct 03 '22 at 04:35

1 Answers1

1

You only need a single capture group, which should capture the first part instead of the digits:

^(.*?[^0-9])[0-9]+

Regex demo

In the replacement use group 1 followed by your replacement text \1[replaced_text]

Example

pattern = r"^(.*?[^0-9])[0-9]+"
s = "/this_text_appears_before/73914774/this_text_appears_after"
 
result = re.sub(pattern, r"\1[replaced_text]", s)
if result:
    print (result)

Output

/this_text_appears_before/[replaced_text]/this_text_appears_after

Other options for the example data can be matching the /

^(.*?/)[0-9]+

Or if you want to match the first 2 occurrences of the /

^(/[^/]+/)[0-9]+
The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • `r"^(.*?[^0-9])[0-9]+"` gets the first group which contains `/this_text_appears_before/` but how this will get the `/this_text_appears_after`? – Maven Oct 03 '22 at 12:01
  • I tried doing `^(.*?[^0-9])[0-9](.*?[^0-9])+` with `\1[replaced_text]\2` but it gives me `/this_text_appears_before/[replaced_text]r` that is only matching the last character of the after string. – Maven Oct 03 '22 at 12:01
  • @Maven You are not altering the last part of the string, so you don't have to capture that and then put it back in the replacement. But if you want 2 capture groups, then `^(.*?[^0-9])[0-9]+(.*)` and then replace with `\1[replaced_text]\2` see https://regex101.com/r/MJkx5l/1 – The fourth bird Oct 03 '22 at 12:18