Jump to content

ReplaceStringInFile with Regex


Recommended Posts

Hi,

I have a problem for which I cannot find a solution as I am a beginner with autoit.

There is a file where I need to replace a line based on a certain pattern.

Here is my file content:

prv("set_user_url","http://website.com/");

And my code:

$find = 'prv("set_user_url","http://website.com/");'

$replace = 'prv("set_user_url","http://new_website.com/");'

$filename = "c:\app_folder\site_builder\cfg\usr.cfg"

_ReplaceStringInFile($filename,$find,$replace)

The problem is that the url of the user config needs to be changed and it is not the same on all users.

So I need to use regex to find the url pattern in that file.

The pattern I need to find is

((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:@=.+?,##%&~-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])

How can I find the pattern and replace the line where it's found?

Thank you.

Link to comment
Share on other sites

Hi LapidusCreatoriu...,

Just a note on Regular Expressions and AutoIt in case you didn't know.

AutoIt uses the PCRE (Perl Compatible Regular Expressions) engine.

If you are using google for tutorials or just searching for specifics, keep this in mind when looking for expressions or help on expressions.

Some things that may help you along with future questions or concerns:

QuickStart

Tutorials

RegExCoach

Link to comment
Share on other sites

If is only line that beginning with prv("set_user_url", then this should work:

#include <File.au3>
$PATH = "c:\app_folder\site_builder\cfg\usr.cfg"
$FILE = FileOpen($PATH,0)
For $INDEX = 1 To _FileCountLines($PATH)
$DATA = FileReadLine($FILE,$INDEX)
If StringLeft($DATA,3) = 'prv("set_user_url",' Then
    _FileWriteToLine($PATH,$INDEX,"'" & $DATA & "'",1)
EndIf
Next
FileClose($FILE)
Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

Hi LapidusCreatoriu...,

Just a note on Regular Expressions and AutoIt in case you didn't know.

AutoIt uses the PCRE (Perl Compatible Regular Expressions) engine.

If you are using google for tutorials or just searching for specifics, keep this in mind when looking for expressions or help on expressions.

Some things that may help you along with future questions or concerns:

QuickStart

Tutorials

RegExCoach

This could be the sign of artificial intelligence.

In the Year of Darkness, 2029, the rulers of this planet devised the ultimate plan. They would reshape the Future by changing the Past. The plan required something that felt no pity. No pain. No fear. Something unstoppable. They created 'THE TERMINATOR'...

I couldn't resist, sorry

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...