1

As I had not explained my question clearly.I will try to explain what I wanna do in my program.

  1. I have created a sqlite database which is build in the python . I have devided table into three kinds of field.A. commandkey B.command C. function[the function of command]

2.To select a sequence of commands from my program, I need to write a function readciscodevice(without regular expression).

  1. This function can print a sequence of commands such as OUTPUT A:
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).

  1. Now I am trying to use regular expression to input some string into (1) and (2) in the output A. this is my expected result after adding regular expression:
    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])
2
  • It's unclear what you're trying to do. None of the regex logic in this code actually has any effect. In fact, it looks like it will always result in a TypeError because of dict[match.group()]. Please explain more clearly what you're trying to do. Commented Apr 19, 2015 at 16:14
  • @jwilner Maybe I try to present it step by step.Update it now Commented Apr 19, 2015 at 17:44

1 Answer 1

1

Try using re.sub(pattern, repl, string) instead of re.compile and finditer, to replace (1) and (2).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.