Sign in to follow this  
Followers 0
LapidusCreatorium

ReplaceStringInFile with Regex

11 posts in this topic

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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Thank you for this automated reply, but I need some help on how to integrate them.

Thank you.

Share this post


Link to post
Share on other sites

File contents are always a single line?


When the words fail... music speaks

Share this post


Link to post
Share on other sites

File contents are always a single line?

No, they have up 100 lines depending on user configuration and settings.

And I only need to find and replace that line.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

all lines start with prv.

Would it better to identify the line by the start letters?

For example to find it by prv("set_user_url" ?

If so, how can I do it?

Share this post


Link to post
Share on other sites

OH. thank you for your code.

I will test it, thanks.

Share this post


Link to post
Share on other sites

Great.

Code worked like a charm.

Thank you very much.

Share this post


Link to post
Share on other sites

Great.

Code worked like a charm.

Thank you very much.

You're welcome. :)

When the words fail... music speaks

Share this post


Link to post
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

Share this post


Link to post
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
Sign in to follow this  
Followers 0