Jump to content

RegExpReplace - Changing file name a line specified in txt


 Share

Recommended Posts

There is a txt file, I want to change the DocumentName section in the

File=    DocumentName.txt line

But all names with .txt are changing.

I tried many pattern but did not succeed.

Here is an example.

$text = "File=    DocumentName.txt" & @CRLF & _
"Test= blabla.txt" & @CRLF & _
"Author=    autoitscript.com" & @CRLF & _
"blabla.txt"

$text = StringRegExpReplace($text, '(?i)([^File=\s+]).*(?=.txt)\.\w{2,3}', "DocumentNameNew.txt")
ConsoleWrite($text & @CRLF)

 

Link to comment
Share on other sites

43 minutes ago, youtuber said:

But all names with .txt are changing.

$text = "File=    DocumentName.txt" & @CRLF & _
"Test= blabla.txt" & @CRLF & _
"Author=    autoitscript.com" & @CRLF & _
"blabla.txt"
$text = StringRegExpReplace($text, '(?i)File=.*?(.txt)', "DocumentNameNew.txt")
ConsoleWrite($text & @CRLF)

@youtuber EDIT : Sorry, but I am wrong. This way File= will also be removed (replaced) :mad2:

Edited by Musashi

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

unfortunately it erases File=   in your pattern :(

File=     part should remain, i just want to change the opposite DocumentName
this is what i want : File=    DocumentNameNew.txt

It would be better if there is a more specific pattern other than that

$text = StringRegExpReplace($text, '(?i)File=.*?(.txt)', "File=    DocumentNameNew.txt")

 

Link to comment
Share on other sites

1 hour ago, youtuber said:

unfortunately it erases File=   in your pattern :(

I had already noticed this myself (see EDIT in my post above).

This approach should work better :

$text = "File=    DocumentName.txt" & @CRLF & _
        "Test= blabla.txt" & @CRLF & _
        "Author=    autoitscript.com" & @CRLF & _
        "blabla.txt"

$text = StringRegExpReplace($text, '(?i)(File=)(\s+).+?(.txt)', '$1$2' & "DocumentNameNew.txt")
ConsoleWrite($text & @CRLF)

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

@AspirinJunkie

You need to include the .txt in your pattern, or invalid file names like "Test" without an extension are matched as well.

Then, asserting that File= has to be at the start of the line, and .txt is at the end of the line, this should do the trick:

StringRegExpReplace($strTestString, '^(File=)\h*(.+\.txt)$', '$1NewDocumentFileName.txt')

^_^

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

2 hours ago, FrancescoDiMuro said:

You need to include the .txt in your pattern, or invalid file names like "Test" without an extension are matched as well.

As i understood his problem is, that with his pattern all *.txt-strings are replaced - even without a "file=" in front of it.
So he only wants a pattern that replace the file name after "File=" regardless of whether they end with .txt
If this is the case than my pattern should still be enough for him.

Link to comment
Share on other sites

Assuming that the file is a .txt and the purpose is only to insert "New" there is another approach

$text = "File=    DocumentName.txt" & @CRLF & _
        "Test= blabla.txt" & @CRLF & _
        "Author=    autoitscript.com" & @CRLF & _
        "blabla.txt"

$text = StringRegExpReplace($text, 'File=.+?\K(?=\.txt)', "New")
msgbox(0,"", $text)

 

Edit
or even like this :D

$text = "File=    DocumentName.txt" & @CRLF & _
        "File=    DocumentName" & @CRLF & _
        "File=    DocumentName.doc" & @CRLF & _
        "Test= blabla.txt" & @CRLF & _
        "Author=    autoitscript.com" & @CRLF & _
        "blabla.txt"

$text = Execute("'" & StringRegExpReplace($text, "File=.+?\K(?=\.txt|\.doc|\R)", "' & 'New' & '") & "'")
msgbox(0,"", $text)

 

Edited by mikell
Link to comment
Share on other sites

1 hour ago, AspirinJunkie said:

So he only wants a pattern that replace the file name after "File=" regardless of whether they end with .txt

I assume, that the extension (here : File= ... .txt) is significant for @youtuber , since he wrote "... (?=.txt)..." in his example. In this case, @FrancescoDiMuro 's objection would be justified.

In the end, @youtuber now has enough variants at his choice, and he can select the most suitable one (EDIT : which he seems to have done in the meantime :lol:.)

Edited by Musashi

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

$text = "File=    DocumentNameA.txt" & @CRLF & _
        "File=    DocumentNameB.txt" & @CRLF & _
        "File=    DocumentNameC.txt" & @CRLF & _
        "File=    DocumentNameD.txt" & @CRLF & _
        "Test= blabla.txt" & @CRLF & _
        "blabla.txt"

;Whatever DocumentName A B C D above I want to replace with ReplaceAllNewFilename
$text = StringRegExpReplace($text, '([File=\s+])DocumentName(?=\.txt)', "ReplaceAllNewFilename")
ConsoleWrite($text & @CRLF)

the result should be like this

File=    ReplaceAllNewFilename.txt
File=    ReplaceAllNewFilename.txt
File=    ReplaceAllNewFilename.txt
File=    ReplaceAllNewFilename.txt

 

Edited by youtuber
Link to comment
Share on other sites

$text = "File=    DocumentNameA.txt" & @CRLF & _
        "File=    DocumentNameB.txt" & @CRLF & _
        "File=    DocumentNameC.txt" & @CRLF & _
        "File=    DocumentNameD.txt" & @CRLF & _
        "Test= blabla.txt" & @CRLF & _
        "blabla.txt"

$text = StringRegExpReplace($text, 'File=.+?\K(DocumentName)(\w.*?)(?=\.txt)', "ReplaceAllNewFilename")
msgbox(0,"", $text)

This also matches e.g. : DocumentName_A.txt, DocumentName01.txt

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

@Musashi

In fact this simple variation does the job

$text = StringRegExpReplace($text, 'File=\h*\K.+?(?=\.txt)', "ReplaceAllNewFilename")

No need to mention any part of the initial filename - what is defined in the pattern after \K is replaced  ;)
 

EDIT
@youtuber

This  ([File=\s+])  is super incorrect !  You are using brackets, meaning a character class
Even with a slightly amended expression like this

'([File=\s+])DocumentName.(?=\.txt)'

the result may seem good, but because of these brackets you include in the replacement one space (which is in the character class) just before "DocumentName" (in the result, there are 3 spaces remaining instead of 4 initially)

Edited by mikell
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...