I have this string
cmd = "show run IP(k1) new Y(y1) add IP(dev.maintserial):Y(dev.maintkeys)"
What is a regex to first match exactly "IP(dev.maintserial):Y(dev.maintkeys)"
I though of something like this:
re.search('(IP\(.*?\):Y\(.*?\))', cmd)
but this will also match the single IP(k1) and Y(y1
My usage will be:
If "IP(*):Y(*)" in cmd:
do substitution of IP(dev.maintserial):Y(dev.maintkeys) to Y(dev.maintkeys.IP(dev.maintserial))
CodePudding user response:
This should work as it is more restrictive. (IP\([^\)] \):Y\(.*?\))
"[^\)] " means at least one character that isn't a closing parenthesis.
".*?" in yours is too open ended allowing almost anything to be in until "):Y("
CodePudding user response:
Something like this?
r"IP\(([^)]*\.. )\):Y\(([^)]*\.. )\)"
You can try it with your string. It matches the entire string IP(dev.maintserial):Y(dev.maintkeys)
with groups dev.maintserial
and dev.maintkeys
.
The RE matches IP(
, zero or more characters that are not a closing parenthesis ([^)]*
), a period .
(\.
), one or more of any characters (.
), then ):Y(
, ... (between the parentheses -- same as above), )
.