Sign in to follow this  
Followers 0
Factfinder

request help with StringRegExpReplace

6 posts in this topic

Hi,

I need to replace some patterns in a text but the StringRegExpReplace is not doing the job if I use $ to match the end of line.

Let's explain what I do.

I have a file (input.txt) with the follwing text:

ab5d;4

TEAM;1 The dirctory is \System32\TEAM

Time;1 The dirctory is %SystemRoot%\Time

Version2;0

The lines without a directory indicate the directory is in the Program Files.

I would like to add "The dierctory is Program files\"Directory Name" (where the "Directory Name" is the one at the start of the line like "ab5d" in the first line):

The desired output would be:

ab5d;4 The dierctory is Program files\ab5d

TEAM;1 The dirctory is \System32\TEAM

Time;1 The dirctory is %SystemRoot%\Time

Version2;0 The dierctory is Program files\Version2

The script I'm using is:

#include <File.au3>
$txt = FileRead("input.txt")
$regexpr = StringRegExpReplace($txt, "(.*);(\d)$", "\1;\2 The dierctory is Program files\\\1")
FileWrite("output.txt" , $regexpr)
Exit

The syntax when used without $ matches all the lines and adds the pattern to all the 4 lines and if used with $ (to exclude the lines with a directory mentioned) gives the same output as input without any modification.

Thanks in advance.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Process it one line at a time and it works perfectly:

#include <File.au3>

Global $sTxt
Global $aTxt[4] = ["ab5d;4", "TEAM;1 The dirctory is \System32\TEAM", _
        "Time;1 The dirctory is %SystemRoot%\Time", "Version2;0"]

For $n = 0 To UBound($aTxt) - 1
    $sTxt = StringRegExpReplace($aTxt[$n], "(.*);(\d)$", "\1;\2 The dierctory is Program files\\\1")
    ConsoleWrite("output.txt:  " & $sTxt & @LF)
Next

Output:

output.txt:  ab5d;4 The dierctory is Program files\ab5d
output.txt:  TEAM;1 The dirctory is \System32\TEAM
output.txt:  Time;1 The dirctory is %SystemRoot%\Time
output.txt:  Version2;0 The dierctory is Program files\Version2
+>10:58:13 AutoIT3.exe ended.rc:0
>Exit code: 0    Time: 1.134

You can process it in a loop with FileReadLine() vice getting it all at once with FileRead().

Or you could just read it into an array with _FileReadToArray() and use the above method exactly.

;)

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Hi PsaltyDS,

Indeed it worked perfectly. ;)

I had spend quite some time to get it work with FileRead() without success. Using FileReadLine() in a loop did the trick and the time delay is negligible even though the input file contains more than 300 lines.

Thanks a lot PsaltyDS. :)

Share this post


Link to post
Share on other sites

$ is not designed to match the end of a line. It matches the end of the string which in this case is the end of the file.

$sStr = FileRead("somefile.txt")
$sRstr = StringRegExp($sStr, "(.+)(;\d)(\v+|$)", "$1$2 The directory is Program files $1\\$3")

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Hi GEOSoft,

Thanks for the reply and the info. $ is used in sed to match the end of line, and now I understand in this case the whole text is considered as a string.

I'll let you know if it worked.

Edited by Factfinder

Share this post


Link to post
Share on other sites

Hi GEOSoft,

It worked like a charm. Thanks a lot. ;)

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