Jump to content

request help with StringRegExpReplace


Factfinder
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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
Link to comment
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!"

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...