Reputation: 59
As I had not explained my question clearly.I will try to explain what I wanna do in my program.
2.To select a sequence of commands from my program, I need to write a function readciscodevice(without regular expression).
enable conf t interface fa(1)/(2) ip address (1) (2)
or OUTPUT B:
enable
conf t
router ospf (1)
network (1) (2)
As you know, the parentheses(1), (2) should have some string inside, hence the command will work after i put ip address and subnet mask in to the first output(A).
interface fa0/1 ip adress 192.168.1.1 255.255.255.0
I should write some raw_input for (1) and (2) but how should I fix the regular expression. Am I writing a wrong way in finditer or use other method. As my english is bad, It is difficult for me to fully understand the website tut to learn Regular expression but at lease I tried to write this re. Please provide some suggestion for me to do this thx.
I have written a function to printout output and some of the output has a (parentheses) that I preferred to re-intput some string into the (parentheses). Now I am trying to use python and regular expression .replace and finditer. But it seems not my cup of tea. So thank you for your help.
This is my output in my database:
interface fa(1)/(2)
ip address (1) (2)
This is my function
def readciscodevice(function, device)://here are some sqlite3 statement
conn = sqlite3.connect('server.db')
cur = conn.cursor()
if device == "switch":
cur.execute(
"SELECT DISTINCT command FROM switch WHERE function =? or function='configure terminal' or function='enable' ORDER BY key ASC",
(function,))
read = cur.fetchall()
return read
elif device == "router":
cur.execute(
"SELECT DISTINCT command FROM router WHERE function =? or function='configure terminal' or function='enable' ORDER BY key ASC",
(function,))
read = cur.fetchall()
return read;
elif device == "showcommand":
cur.execute(
"SELECT DISTINCT command FROM showcommand WHERE function =? or function='enable' ORDER BY key ASC",
(function,))
read = cur.fetchall()
return read;
a = input("function:") //First, I input function field from database
b = input("device:") //I need to input the name of my elif =="name"
p = re.compile('\(.*?\)') //I am not sure what it is doing...
s = "1"
iterator = p.finditer(s) //finditer is suitable to replace parentheses?
for match in iterator:
s = s[:match.start()] + s[match.start():match.end()].replace(match.group(), dict[match.group()]) + s[match.end()]
for result in readciscodevice(a,b):
print(result[0])
Upvotes: 1
Views: 117
Reputation: 254
Try using re.sub(pattern, repl, string)
instead of re.compile
and finditer
, to replace (1) and (2).
Upvotes: 1