youtuber Posted January 24, 2021 Share Posted January 24, 2021 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 More sharing options...
Musashi Posted January 24, 2021 Share Posted January 24, 2021 (edited) 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) Edited January 24, 2021 by Musashi "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 More sharing options...
youtuber Posted January 24, 2021 Author Share Posted January 24, 2021 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 More sharing options...
Musashi Posted January 25, 2021 Share Posted January 25, 2021 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) youtuber 1 "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 More sharing options...
AspirinJunkie Posted January 25, 2021 Share Posted January 25, 2021 Shorter and (maybe) more simple: $text = "File= DocumentName.txt" & @CRLF & _ "Test= blabla.txt" & @CRLF & _ "Author= autoitscript.com" & @CRLF & _ "blabla.txt" $text = StringRegExpReplace($text, 'File=\s*\K(.+)', "DocumentNameNew.txt") ConsoleWrite($text & @CRLF) youtuber 1 Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted January 25, 2021 Share Posted January 25, 2021 (edited) @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 January 25, 2021 by FrancescoDiMuro AspirinJunkie 1 Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
AspirinJunkie Posted January 25, 2021 Share Posted January 25, 2021 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. FrancescoDiMuro 1 Link to comment Share on other sites More sharing options...
mikell Posted January 25, 2021 Share Posted January 25, 2021 (edited) 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 $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 January 25, 2021 by mikell Musashi and youtuber 1 1 Link to comment Share on other sites More sharing options...
Musashi Posted January 25, 2021 Share Posted January 25, 2021 (edited) 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 .) Edited January 25, 2021 by Musashi FrancescoDiMuro and youtuber 2 "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 More sharing options...
youtuber Posted January 25, 2021 Author Share Posted January 25, 2021 (edited) $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 January 25, 2021 by youtuber Link to comment Share on other sites More sharing options...
Musashi Posted January 25, 2021 Share Posted January 25, 2021 $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 "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 More sharing options...
mikell Posted January 25, 2021 Share Posted January 25, 2021 (edited) @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 January 25, 2021 by mikell youtuber and Musashi 1 1 Link to comment Share on other sites More sharing options...
youtuber Posted January 25, 2021 Author Share Posted January 25, 2021 Great thanks This is great:$text = StringRegExpReplace($text, 'File=\h*\K.+?(?=\.txt)', "ReplaceAllNewFilename") Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now