Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using python's re.sub regular expressions to replace strings

I want to find and replace the following code snippet in java code.

::[Node1]N81:157-->::[Node1]N81[157]
::[Node1]B81:72/0-->::[Node1]B81[72].0

157 and 72 and 0 could be dynamic so maybe could have other values.

I have some pattern to find my expression but I don't know if I could improve it. Anyway, I don't know how to replace I only know the way to find the pattern as follows:

re.sub("::\[Node1]N[0-9]+:[0-9]+",'here I should put how to replace' , s)       
re.sub("::\[Node1]B[0-9]+:[0-9]+/[0-9]+",'here I should put how to replace' , s)
like image 224
Jmm86 Avatar asked Mar 27 '26 21:03

Jmm86


1 Answers

Use a capturing group:

>>> re.sub(r'::\[Node1]B(\d+):(\d+)/(\d+)', r'::[Node1]B\1[\2].\3', s)
'::[Node1]B81[72].0'
like image 176
L3viathan Avatar answered Mar 29 '26 12:03

L3viathan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!