soft4pedia

Pattern to replace similar character using StringReplace Function

13 posts in this topic

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", "")

 

Share this post


Link to post
Share on other sites



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.

Share this post


Link to post
Share on other sites

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)

 

Share this post


Link to post
Share on other sites

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)

 

1 person likes this

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

#7 ·  Posted (edited)

:D

$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 by mikell
1 person likes this

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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)

 

Share this post


Link to post
Share on other sites

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)

 


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

Share this post


Link to post
Share on other sites

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)  :D

Share this post


Link to post
Share on other sites

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)

 


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

Share this post


Link to post
Share on other sites

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)

 

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