Jump to content

StringRegExpReplace Help


JimJ
 Share

Recommended Posts

Good afternoon all.

After hours of searching, I have swallowed my ego to ask this very simple question.

I have this line in a file:  <CompanySite key="001|01|4418">  I just want to change the 001 part of this line in the file....the rest is needed to stay the same.

I have this line in an AutoIt script:  StringRegExpReplace($sLine,"001","714")

When I execute the process, it will not change the 001 value to 714.  And when I check the expression with $ChkRegEx = StringRegExp($sLine,'001','714',0), it returns a value of 1, which I believe indicates that it's a good expression.

Truly grateful for any help....just can't figure this one out.

Jim

Link to comment
Share on other sites

11 minutes ago, JimJ said:

When I execute the process, it will not change the 001 value to 714.

Yes it does!

Local $sLine = '<CompanySite key="001|01|4418">'
; your way
Local $sChanged = StringRegExpReplace($sLine, "001", "714")
ConsoleWrite($sChanged & @LF)
; another skin for this cat
$sChanged = StringReplace($sLine, "001", "714")
ConsoleWrite($sChanged & @LF)

 

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

Post your code (as always).

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

#include <File.au3>
#include <Array.au3>
#include <String.au3>
#include <MsgBoxConstants.au3>
#include <AutoItConstants.au3>

Global $file = "C:\TestFolder\TestFile.cfg", $search = "<CompanySite key="
Global $iLine = 0, $sLine = '', $iValid = 0
Global $hFile = FileOpen($file)
If $hFile = -1 Then
    MsgBox(0, 'ERROR', 'Unable to open file for reading.')
    Exit 1
EndIf

; find the line that has the search string
While 1
    $iLine += 1
    $sLine = FileReadLine($hFile)
    If @error = -1 Then ExitLoop

    ; $search found in the line, replace index number
    If StringInStr($sLine, $search) And Not $iValid Then
        $ChkRegEx = StringRegExp($sLine,'001','714',0)
        StringRegExpReplace($sLine,'001','714')

ExitLoop
    EndIf
WEnd
FileClose($hFile)

Link to comment
Share on other sites

so you open the file in read mode, and then try and write something to it?

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

6 minutes ago, iamtheky said:

so you open the file in read mode, and then try and write something to it?

I thought that StringRegExpReplace($sLine,"001","714") automatically made the change to file?  Do I have to utilize a write process in there?

Link to comment
Share on other sites

11 minutes ago, mikell said:

Even with my best glasses I can't see the place in this code where the new text is written in the file ...

Please forgive the ignorance of these simple questions....I really am a newb and am just learning:  but how would I write back just the change to the file (001 to 714) and leave the rest of the line intact?

Link to comment
Share on other sites

Several ways to do that. Basically the usual way is : read the file content to a string or an array, modify this string/array, then write it back to the file
So you have to look at the various FileOpen modes (read, write, overwrite) and so on
You also can : FileReadLine, modify the line, _FileWriteToLine ...

The helpfile is your best friend  ;)

Edited by mikell
Link to comment
Share on other sites

3 minutes ago, mikell said:

Several ways to do that. Basically the usual way is : read the file content to a string or an array, modify this string/array, then write it back to the file
So you have to look at the various FileOpen modes (read, write, overwrite) and so on
You also can : FileReadLine, modify the line, _FileWriteToLine ...

The helpfile is your best friend  ;)

Thank you...appreciate the insight...but honestly, spent a lot of time looking and cannot figure out how to write back just the 001 to 714 part without messing up the line.  I'll keep working on it.

This is the example from the help file....demonstrates basic replacement....everything I was looking for....but has no "write" designation...which is why I thought it just updated the file on it's own.

#include <MsgBoxConstants.au3>

Test1()
Test2()
Test3()

; This example demonstrates a basic replacement.  It replaces the vowels aeiou
; with the @ character.
Func Test1()
    Local $sInput = "Where have all the flowers gone, long time passing?"
    Local $sOutput = StringRegExpReplace($sInput, "[aeiou]", "@")
    Display($sInput, $sOutput)
EndFunc   ;==>Test1
Link to comment
Share on other sites

I feel foolish and embarrassed to write this....but I figured it out.  It's the whole writing to file thing...I WAY over complicated this thing, got caught up in the weeds, and failed simple logic.  I'm sorry to waste your time.  But thanks for the tough love and for making me think it through.

Link to comment
Share on other sites

39 minutes ago, JimJ said:

I feel foolish and embarrassed to write this...

nah, you can save that for when you do it the sixth time.  Most of the common failure points are learned by feel (mostly the feeling of being berated on the forum).

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

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