Jump to content

How to do StringRegExpReplace or StringReplace Incrementally?


HezzelQuartz
 Share

Recommended Posts

If I have a txt file containing text below

X0  00112233
X1  AABBCCDD
X2  22446688
X3  BBDDFF00
X4  11335577
X5  AACCEEFF
X6  11AA22BB
X7  CC33DD44
X8  55EE66FF
X9  ABCD9988
X10 8800AADD
X11 EEFF3344
X12 6622AAEE
X13 5599CCFF
X14 AAFF2200
X15 0099AACC


Then, I want to change it to be below

X0  00112033
X1  AABBC1DD
X2  22446288
X3  BBDDF300
X4  11335477
X5  AACCE5FF
X6  11AA26BB
X7  CC33D744
X8  55EE68FF
X9  ABCD9988
X10 8800AADD
X11 EEFF3B44
X12 6622ACEE
X13 5599CDFF
X14 AAFF2E00
X15 0099AFCC

So I want to change the third hex character from right incrementally like these:

1st line: 0

2nd Line: 1

15th line: F

16th line: 0 (back to 0)

How can I do that?

I try to create script like this:

$text = FileRead(@ScriptDir & "\" & "example.txt")
$hex = StringRegExpReplace($text, 'X\d+\h[[:xdigit:]]{5}\K([[:xdigit:]]{1})', 'F')
FileWrite($locscript & "newexample.txt", $hex)

I just confused what to do to change the replaced string in every line

Thank You

Sorry for bad explanation

Link to comment
Share on other sites

old school without regexp :)

#include <Array.au3>

Local $text = FileRead(@ScriptDir & "\example.txt")
Local $aList = StringSplit($text, @CRLF, 1)
;~ _ArrayDisplay($aList)
Local $Newtext
Local $idx = 16
For $i = 1 To $aList[0]

    ConsoleWrite($i & ") " & $aList[$i] & " -> ")

    Local $front = StringTrimRight($aList[$i], 3)
    Local $back = Hex($idx, 1) & StringRight($aList[$i], 2)

    ConsoleWrite($front & $back & @CRLF)

    $Newtext &= $front & $back & @CRLF
    $idx += 1
    If $idx = 17 Then $idx = 1
Next

$Newtext = StringTrimRight($Newtext, 2)

$newexample = @ScriptDir & "\newexample.txt"
If FileExists($newexample) Then FileDelete($newexample)
FileWrite($newexample, $Newtext)

 

Edited by ioa747
correction

I know that I know nothing

Link to comment
Share on other sites

Regex school:

Local $text = FileRead(@ScriptDir & "\" & "source.txt")
Local $hex = Execute('"' & StringRegExpReplace($text, '(?m)(?<=[[:xdigit:]]{5})([[:xdigit:]])(?=[[:xdigit:]]{2})', '" & _IncHex() & "') & '"')
ConsoleWrite($hex)

Func _IncHex()
    Local Static $Inc = 15
    $Inc += 1
    Return Hex(Mod($Inc, 16), 1)
EndFunc

I added two extra line in the source file, yielding:
 

X0  00112033
X1  AABBC1DD
X2  22446288
X3  BBDDF300
X4  11335477
X5  AACCE5FF
X6  11AA26BB
X7  CC33D744
X8  55EE68FF
X9  ABCD9988
X10 8800AADD
X11 EEFF3B44
X12 6622ACEE
X13 5599CDFF
X14 AAFF2E00
X15 0099AFCC
X16 FD5A00B6
X17 10E51194

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

4 hours ago, ioa747 said:

old school without regexp :)

#include <Array.au3>

Local $text = FileRead(@ScriptDir & "\example.txt")
Local $aList = StringSplit($text, @CRLF, 1)
;~ _ArrayDisplay($aList)
Local $Newtext
Local $idx = 16
For $i = 1 To $aList[0]

    ConsoleWrite($i & ") " & $aList[$i] & " -> ")

    Local $front = StringTrimRight($aList[$i], 3)
    Local $back = Hex($idx, 1) & StringRight($aList[$i], 2)

    ConsoleWrite($front & $back & @CRLF)

    $Newtext &= $front & $back & @CRLF
    $idx += 1
    If $idx = 17 Then $idx = 1
Next

$Newtext = StringTrimRight($Newtext, 2)

$newexample = @ScriptDir & "\newexample.txt"
If FileExists($newexample) Then FileDelete($newexample)
FileWrite($newexample, $Newtext)

 

Thank You..

I tried and I think it works well

Link to comment
Share on other sites

2 hours ago, jchd said:

Regex school:

Local $text = FileRead(@ScriptDir & "\" & "source.txt")
Local $hex = Execute('"' & StringRegExpReplace($text, '(?m)(?<=[[:xdigit:]]{5})([[:xdigit:]])(?=[[:xdigit:]]{2})', '" & _IncHex() & "') & '"')
ConsoleWrite($hex)

Func _IncHex()
    Local Static $Inc = 15
    $Inc += 1
    Return Hex(Mod($Inc, 16), 1)
EndFunc

I added two extra line in the source file, yielding:
 

X0  00112033
X1  AABBC1DD
X2  22446288
X3  BBDDF300
X4  11335477
X5  AACCE5FF
X6  11AA26BB
X7  CC33D744
X8  55EE68FF
X9  ABCD9988
X10 8800AADD
X11 EEFF3B44
X12 6622ACEE
X13 5599CDFF
X14 AAFF2E00
X15 0099AFCC
X16 FD5A00B6
X17 10E51194

 

Thank You

I tried and I think it works well

But

Could you please explain what is the function of execute?

I read in the help file but I'm still confused...

I just saw execute: "Execute an expression."

Thank You

Link to comment
Share on other sites

The string supplied to Execute is AutoIt code, which is then executed. This is the way to execute dynamically created code.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Or from the hybrid school :

#include <Array.au3>

$aText = FileReadToArray("Test.txt")
For $i = 0 To UBound($aText) - 1
  $aText[$i] = StringRegExpReplace($aText[$i], "(?<=[[:xdigit:]]{5})([[:xdigit:]])(?=[[:xdigit:]]{2})", Hex(Mod(Number(StringRegExp($aText[$i], "X(\d+)", 1)[0]), 16), 1))
Next
ConsoleWrite(_ArrayToString($aText, @CRLF) & @CRLF) ; or _FileWriteFromArray

:)

Link to comment
Share on other sites

I found the Mod() function very useful

old school with hybrid influences :)

#include <Array.au3>

$aList = FileReadToArray("example.txt")
For $i = 0 To UBound($aList) - 1
    $aList[$i] = StringTrimRight($aList[$i], 3) & Hex(Mod($i + 16, 16), 1) & StringRight($aList[$i], 2)
Next
ConsoleWrite(_ArrayToString($aList, @CRLF) & @CRLF) ; or _FileWriteFromArray

 

Edited by ioa747

I know that I know nothing

Link to comment
Share on other sites

From the simple school:

FileCopy("example.txt", "newexample.txt", 1)
$file = FileOpen("newexample.txt", 1 + 16)
FileSetpos($file, 9, 0)
For $loop = 0 To 15
    FileWrite($file, Hex($loop, 1))
    FileSetpos($file, 13, 1)
Next
FileClose($file)

 

Edited by Werty
added filecopy

Some guy's script + some other guy's script = my script!

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