soft4pedia Posted March 7, 2016 Share Posted March 7, 2016 Hello, I'm having several names of books those are ended with the almost same pattern (by Van Horne), (by Philip Crosby) (by Adam Smith), etc... My questions is how can i use StringReplace command once to replace all ending names of authors like I don't want ending character after "by" and onward. I try to study StringReplace function very deeply but unable to build any suitable pattern for this. Guide me please $File1 = @scriptdir & "\replace.txt" $txt = FileRead($File1) $txt = StringReplace($txt, "by Van Horne", "") $txt = StringReplace($txt, "by Johnson T", "") Link to comment Share on other sites More sharing options...
JohnOne Posted March 7, 2016 Share Posted March 7, 2016 StringRegExReplace AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
Cormin Posted March 7, 2016 Share Posted March 7, 2016 StringRegExpReplace ( "test", "pattern", "replace" [, count = 0] ) https://www.autoitscript.com/autoit3/docs/functions/StringRegExpReplace.htm Here ya go Link to comment Share on other sites More sharing options...
soft4pedia Posted March 7, 2016 Author Share Posted March 7, 2016 Hello, I know how to use this command but my problem is that I don't know how to create pattern even i study the a lot and there are lot of ways to make pattern but those are not upto to my understanding please guide me which patter will be ok for me to replace everything from character "by" and onward. Link to comment Share on other sites More sharing options...
mikell Posted March 7, 2016 Share Posted March 7, 2016 Here it is, but be careful if "by" is present in the title $txt = "my book 1 by Van Horne " & @crlf & _ "my book 2 by Adam Smith 2 " & @crlf & _ "my book 3 by Philip Crosby Junior (part 2)" Msgbox(0,"", $txt) $txt = StringRegExpReplace($txt, 'by.+', "") Msgbox(0,"", $txt) Link to comment Share on other sites More sharing options...
BrewManNH Posted March 7, 2016 Share Posted March 7, 2016 This might be a slightly better pattern. $txt = "my bytes 1 by Van Horne " & @crlf & _ "my book 2 by Adam Smith 2 " & @crlf & _ "my book 3 by Philip Crosby Junior (part 2)" $txt = StringRegExpReplace($txt, ' by .+', "") Msgbox(0,"", $txt) soft4pedia 1 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 GudeHow 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 Link to comment Share on other sites More sharing options...
mikell Posted March 7, 2016 Share Posted March 7, 2016 (edited) $txt = "my book 1 by Van Horne " & @crlf & _ "bye bye 2 by Adam Smith 2 " & @crlf & _ "stay by my side 3 by Philip Crosby Junior (part 2)" ;Msgbox(0,"", $txt) $txt = StringRegExpReplace($txt, '(?m)^(.+)\hby\h.+', "$1") Msgbox(0,"", $txt) Edited March 7, 2016 by mikell BrewManNH 1 Link to comment Share on other sites More sharing options...
soft4pedia Posted March 7, 2016 Author Share Posted March 7, 2016 This pattern really works for me...Thanks I got another situation where the names of authors just be placed inside torrent brackets such as; Title Book 1 [Adam Smith] Title Book 1 {Pete Anderson} Title Book 1 (Anna William) Title Book 1 - Andrew Clark only these 4 situation are commonly used in titles of my books... In this situation which pattern is more suitable so i can use single pattern to replace all, please Link to comment Share on other sites More sharing options...
mikell Posted March 7, 2016 Share Posted March 7, 2016 Wow, changes are coming ... $txt = "Title Book 1 [Adam Smith] " & @crlf & _ "Title Book 2 {Pete Anderson} " & @crlf & _ "Title Book 3 (Anna William) " & @crlf & _ "Title Book 4 - Andrew Clark " ;Msgbox(0,"", $txt) $txt = StringRegExpReplace($txt, '(?m)^(.+)\h[\[{\(-].+', "$1") Msgbox(0,"", $txt) Link to comment Share on other sites More sharing options...
iamtheky Posted March 7, 2016 Share Posted March 7, 2016 yay, i get to edge case mikell instead of vice versa $txt = "Title Book 1 [Adam Smith] " & @crlf & _ "Title Book 2 {Pete Anderson} " & @crlf & _ "Title Book 3 (Anna William) " & @crlf & _ "Hyphenated-Title Book 4 - Andrew Clark " ;Msgbox(0,"", $txt) $txt = StringRegExpReplace($txt, '(?m)^(.+)\h[\[{\(-].+', "$1") Msgbox(0,"", $txt) ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
mikell Posted March 7, 2016 Share Posted March 7, 2016 iamtheky, Sorry... I can't begin to consider all possible edge cases because I need to sleep a little before going back to work tomorrow morning (it's 23:00 now in my country) Link to comment Share on other sites More sharing options...
iamtheky Posted March 7, 2016 Share Posted March 7, 2016 Here it is in a slower, but maybe more manageable format as you evaluate cases that slip through #include<array.au3> local $aTxt = ["my bytes 1 by Van Horne " , _ "my book 2 by Adam Smith 2 " , _ "my book 3 by Philip Crosby Junior (part 2)" , _ "Title Book 4 [Adam Smith]", _ "Title Book 5 {Pete Anderson}", _ "Title Book 6 (Anna William)", _ "Title Book 7 - Andrew Clark-Griswold"] ;~ _ArrayDisplay($aTxt) For $i = 0 to ubound($aTxt) - 1 If StringinStr($aTxt[$i] , " by " , 0 , -1) Then $aTxt[$i] = StringReplace($aTxt[$i] , stringmid($aTxt[$i] , StringinStr($aTxt[$i] , " by " , 0 , -1) , -1) , " ") If StringinStr($aTxt[$i] , " [" , 0 , -1) Then $aTxt[$i] = StringReplace($aTxt[$i] , stringmid($aTxt[$i] , StringinStr($aTxt[$i] , " [" , 0 , -1) , -1) , " ") If StringinStr($aTxt[$i] , " {" , 0 , -1) Then $aTxt[$i] = StringReplace($aTxt[$i] , stringmid($aTxt[$i] , StringinStr($aTxt[$i] , " {" , 0 , -1) , -1) , " ") If StringinStr($aTxt[$i] , " (" , 0 , -1) Then $aTxt[$i] = StringReplace($aTxt[$i] , stringmid($aTxt[$i] , StringinStr($aTxt[$i] , " (" , 0 , -1) , -1) , " ") If StringinStr($aTxt[$i] , " - " , 0 , -1) Then $aTxt[$i] = StringReplace($aTxt[$i] , stringmid($aTxt[$i] , StringinStr($aTxt[$i] , " - " , 0 , -1) , -1) , " ") Next _ArrayDisplay($aTxt) ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
jguinch Posted March 8, 2016 Share Posted March 8, 2016 Also, you can combine both cases : $txt = "Title Book 1 [Adam Smith] " & @crlf & _ "Title Book 2 {Pete Anderson} " & @crlf & _ "Title Book 3 (Anna William) " & @crlf & _ "Title Book 4 - Andrew Clark " & @crlf & _ "stay by my side 3 by Philip Crosby Junior (part 2)" $txt = StringRegExpReplace($txt, '(.+\K\hby|.+\K\h[\[({-]).+', "") Msgbox(0,"", $txt) Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF 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