Sign in to follow this  
Followers 0
Sam137

Help with "StringRegExp" function

20 posts in this topic

#1 ·  Posted (edited)

Hi Guys

Can you please help me out with the pattern of the one which is below:

Pattern to look:

"filepath = "

There are 16 spaces between "filepath" and "= "

If StringRegExp($sStr, "(?im)^(filepathh*=h*[^v]+)") Then
    $sNewStr = StringRegExpReplace($sStr, "(?im)^(filepath)(h*=h*)([^v]+)", "$1$2" & _
      StringRegExpReplace("C:Multi DDt Delete Encounter MPIcleanup.in", "()", ""))

$sStr is the file which is returned from the Fileread command.

I Just want to know how the code the pattern.

Edited by Sam137

Share this post


Link to post
Share on other sites



Do you know "StringStripWS()"?


Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
Share on other sites

StringStripWS will remove WS from inside the path as well, I'm afraid.

@Sam137,

What does you input exactly look like and what do you expect as output, precisely?


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)

Share this post


Link to post
Share on other sites

Okay well,

I am searching for the below string in the file.

"filepath****************= "

If this is found then i want to replace it with:

"filepath****************= C:Multi DDt Delete Encounter MPIcleanup.in"

NOTE: Please consider all * as spaces. For that i have the below coding. But its not working.

If StringRegExp($sStr, "(?im)^(filepathh*=h*[^v]+)") Then
    $sNewStr = StringRegExpReplace($sStr, "(?im)^(filepath)(h*=h*)([^v]+)", "$1$2" & _
      StringRegExpReplace("C:Multi DDt Delete Encounter MPIcleanup.in", "()", ""))

Share this post


Link to post
Share on other sites

OK, and why not use StringReplace only?


Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
Share on other sites

Well I can use String Replace, but the problem is :

Actual whole string in the file is: "filepath****************= e:autitMulti Encounter MPIcleanup.in"

First thing i want to search for the string:

"filepath****************= " Remember * as spaces and then i delete the remaining part of the string i.e."e:autitMulti Encounter MPIcleanup.in" then i replace with "filepath****************= C:Multi DDt Delete Encounter MPIcleanup.in". I think it will be a tedious job in string Replace. Any suggestions?

Share this post


Link to post
Share on other sites

Maybe I don't understand your problem right, but why not

StringReplace($sStr, "e:autitMulti Encounter MPIcleanup.in", "C:Multi DDt Delete Encounter MPIcleanup.in")

??


Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
Share on other sites

"e:autitMulti Encounter MPIcleanup.in" is not consistent with all the files it keeps on changing, the only thing which is consistent is :

"filepath****************="

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

sam137,

You are not defining your requirements logically or consistently. Give an example of 10+ filenames and what you need to change them to.

kylomas

Edit: spelling

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Ok, my requirement is:

I have a folder which contains 'n' number of files. I want to open the first file and search for the string "filepath****************= " Remember * as spaces and then i delete the remaining part of the string i.e."e:\autitMulti\ Encounter MPI\cleanup.in" , after deleting the second fragment we have only left with "filepath****************= " then i replace with "filepath****************= C:\Multi DDt Delete Encounter MPI\cleanup.in".

See an example of the file in the attachment.

Example.txt

Share this post


Link to post
Share on other sites

Does this work as you expect?

Local $text = FileRead("Example.txt")
Local $newtext = StringRegExpReplace($text, "(?im)(^s*filepath[^=]*=)(.*)", "$1C:Multi DDt Delete Encounter MPIcleanup.in")
ConsoleWrite($newtext & @LF)
Local $hdl = FileOpen("Changed.txt", 2)
FileWrite($hdl, $newtext)
FileClose($hdl)

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)

Share this post


Link to post
Share on other sites

I tried code from jchd and I discovered it needs doubled to work correctly:

Local $newtext = StringRegExpReplace($text, "(?im)(^s*filepath[^=]*=)(.*)", "$1C:Multi DDt Delete Encounter MPIcleanup.in")

Share this post


Link to post
Share on other sites

I believe the script in post #1 works perfectly.

If the file being read (with FileRead()) is not in the same directory or folder as the script that you are using then the full path of the file to be read (or a path relative to the script) should be used in the FileRead() function.

Because I had the test ".in" file in the same directory as the script this has always worked for me.

See the modified script see if the results returned are as per your requirements.

Share this post


Link to post
Share on other sites

@Zedna

Sure backslashes need doubling there (reference to captured groups may use $1 or 1 syntax.) They're doubled in my copy. I don't get it how they got de-doubled magically during copy/paste.


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)

Share this post


Link to post
Share on other sites

jchd,

It is this wonderful "upgraded" editor we now have. :D

I use it in basic mode all the time - the WYSIWYG version is just too flaky. :oops:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Oh shitty modernity.


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)

Share this post


Link to post
Share on other sites

@jchd - Thanks for the help.

I like to get the information how to modify it when i have a 2 or more strings to seach and replace with another.

Currently what i have given is to search only for "filepath****************= ". Now i have more strings to search for example "logpath****************= " and "errorpath****************= ". The problem is I dont have a starndard replacement string for all the three.

In one single file:

"filepath****************= " has to be replaced by "C:Multi DDt Delete Encounter MPIcleanup.in"

"logpath****************= " has to be replaced by "D:Testtestup.in"

"errorpath****************= " has to be replaced by "E: Encounter MPItesting.in"

how can i change the below string to cater this.

Local $newtext = StringRegExpReplace($text, "(?im)(^s*filepath[^=]*=)(.*)", "$1C:Multi DDt Delete Encounter MPIcleanup.in")

Share this post


Link to post
Share on other sites

Last try from my side:

#Include <File.au3>

Global $aFile

_FileReadToArray("Example.txt", $aFile)

For $i = 1 To $aFile[0]
    If StringInStr($aFile[$i], "filepath****************=") Then $aFile[$i] = "filepath****************= C:Multi DDt Delete Encounter MPIcleanup.in"
    If StringInStr($aFile[$i], "logpath****************=") Then $aFile[$i] = "logpath****************= D:Testtestup.in"
    If StringInStr($aFile[$i], "errorpath****************=") Then $aFile[$i] = "errorpath****************= E: Encounter MPItesting.in"
Next

_FileWriteFromArray("Example_new.txt", $aFile, 1)

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
Share on other sites

But the alignment is getting changed. Its going to the first position. I would want something like in the attacheed file.

Example.txt

Share this post


Link to post
Share on other sites

That's a standard ini file, are you absolutely 100% sure you need the spaces to remain in the file when it's read by whatever needs to read that file? If not, you can probably just use Iniwrite to rewrite those values, and you won't need any regexp to do it. You might even be able to do it with the spacing, but I don't guarantee that it will keep the spacing.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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