regex - Replace [1-2] in to 1 in Python -


i replace [1-2] 1, [3-4] 3, [7-8] 7, [2] 2, , on.

for example, use following strings:

db[1-2].abc.xyz.pqr.abc.abc.com db[3-4].abc.xyz.pqr.abc.abc.com db[1].abc.xyz.pqr.abc.abc.com xyz-db[1-2].abc.xyz.pqr.abc.abc.com 

and convert them to

db1.abc.xyz.pqr.abc.abc.com db3.abc.xyz.pqr.abc.abc.com db1.abc.xyz.pqr.abc.abc.com xyz-db1.abc.xyz.pqr.abc.abc.com 

you use regex like:

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

and replace with:

$1$2$3 

here's regex does:

^ matches beginning of string
(.*) matches character amount of times, , first capture group
\[ matches character [ literally
([0-9]+) matches number 1 or more times, , second capture group
.*? matches character amount of times, tries find smallest match
\] matches character ] literally
(.*) matches characters amount of times
$ matches end of string

by replacing $1$2$3, replacing text in first capture group, followed text in second capture group, followed text in third capture group.

here's live preview on regex101.com


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -