How to find a word and then replace the text after it with regular expressions in python?

I am trying to write a script that will search through an html file and then replace the form action. So, in this base code:

<html>
    <head>
        <title>Forms</title>
    </head>
    <body>
    <form action="login.php" method="post">
        Username: <input type="text" name="username" value="" />
        <br />
        Password: <input type="password" name="password" value="" /> 
        <br />
        <input type="submit" name="submit" value="Submit">
    </form>
    </body>
</html>

I would like the script to search for the form action = "login.php", but only replace login.php, say newlogin.php. The main thing is that the action of the form can vary from file to file, that is, in another html file login.php can be something completely different, so the regular expression should look for the form action = and replace the text after it (possibly using "as delimiters?"

My knowledge of regular expressions is pretty simple, for example, I would know how to replace only login.php:

(re.sub('login.php', 'newlogin.php', line))

but obviously it is not used, as indicated above, if login.php changes from file to file.

!

=)

+5
3

re catch 2 , , 1- , .

1- , :

re.sub(r'(<form.*?action=")([^"]+)', r'\1newlogin.php',  content)
+1

. .

for line in open("file"):
    if "form action" in line:
       line=line.rstrip()
       a=line.split('<form action="')
       a[-1] = '"newlogin" ' + a[-1].split()[-1]
       line = '<form action='.join(a)
    print line
+2

You cannot try this technique:

(<form[^>]*action=")[^"]*

pseudo code:

regex.replace(input, pattern, concat(\1, new_value))

You can use this regex:

(?<=<form[^>]*action=")[^"]*
0
source

All Articles