Move matching text from end of line, to start of line Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
siomosp
Posts: 27
Joined: 18 Jan 2022, 10:21

Move matching text from end of line, to start of line

Post by siomosp » 02 Mar 2022, 18:35

Hi!
It is possible to move specific text form line end to start ?
At following example, i want to move "sample text" to the start of line.
"this is nice, sample text"
I want to convert it to
"sample text , this is nice"
(My actual text is copied from webpage, to clipboard)

User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Move matching text from end of line, to start of line

Post by mikeyww » 02 Mar 2022, 19:48

I believe that your current description is inadequate, because it does not explain, comprehensively and in detail, exactly how to handle punctuation. A script could be written to handle your individual specific example as posted, but the question would then quickly arise, "Yes, but what if....?" Thus, my suggestion is for you write a list of every rule that would be applied, to get from input to output. This task in itself requires no knowledge of coding. The rules can then be transformed into code.

siomosp
Posts: 27
Joined: 18 Jan 2022, 10:21

Re: Move matching text from end of line, to start of line

Post by siomosp » 03 Mar 2022, 03:41

Thank you mikeyww for the reply!
I want to use the result for generating (for e-shop product), seo friendly url field.
I don΄t care for punctuation, the whole line will be converted again, using build in php function
I only need a space between moved text and line start.
The texts I want to move from end to front are about 10, "canvas print", "wall stickers", "Room divider"
At following examples i need only the "Script result"
Detailed example:

Product tittle:
Celebration of July in Paris, van Gogh Vincent, canvas print ; I want to move the the text "canvas print " from end, to front
Script result:
canvas print Celebration of July in Paris, van Gogh Vincent,
Build in php function result
canvas-print-celebration-of-july-in-paris-van-gogh-vincent

Another example

Product tittle:
Blooming cherry, pink, white flowers wall stickers ; I want to move the the text " wall stickers" from end, to front
Script result:
wall stickers Blooming cherry, pink, white flowers
Build in php function result
wall-stickers-blooming-cherry-pink-white-flowers


I will paste the Script result to my eshop software, Friedly url generator, and the build in php function will convert it
It removes commas, replaces spaces with minus etc
I hope description is better now!

Actual product links
https://www.stickit.gr/en/canvas-prints/fine-art-prints/global-artists/canvas-print-celebration-of-july-in-paris-van-gogh-vincent
https://www.stickit.gr/en/wall-stickers/nature/wall-stickers-blooming-cherry-pink-white-flowers

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Move matching text from end of line, to start of line

Post by BoBo » 03 Mar 2022, 04:01

to move specific text
From where to receive the info on what text/words should be swapped?
The below script will take a string that has been copied to the clipboard (press Alt+c) and converts it as requested.
Output is copied to the clipboard.

Code: Select all

#SingleInstance, Force

;str := "Celebration of July in Paris, van Gogh Vincent, canvas print"
;str := "Blooming cherry, pink, white flowers wall stickers"


!c::												; press Alt+c
	Send ^c											; copy whatever text you've selected to the clipboard
	ClipWait										; wait till clipboard contains content
	if (clipboard="")
		Return
	StringLower, clipboard, str						; convert text to lower case
	arr := StrSplit(StrReplace(str,",",""),A_Space)	; remove commas, split at whatever space character
	url := arr[arr.Count()-1] "-" arr[arr.Count()]	; set the last two words to the front

	Loop % arr.Count()-2							; loop for array items (except the last two)
		url .= "-" arr[A_Index]						; add the remaining words
	Msgbox % clipboard := url						; copy converted str to clipboard
	Return

just me
Posts: 9574
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Move matching text from end of line, to start of line

Post by just me » 03 Mar 2022, 06:23

Code: Select all

#NoEnv
Terms := "canvas print|wall stickers|room divider" ; search terms divided by pipe |
Titles := ["Celebration of July in Paris, van Gogh Vincent, canvas print"
         , "Blooming cherry, pink, white flowers wall stickers"
         , "Any other arbitrary text."]
For Each, Title In Titles {
   Result := RegExReplace(Title, "i`a)(.*)(" . Terms . ")$", "$2 $1")
   MsgBox, %Title%`n->`n%Result%
}
ExitApp
?

User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Move matching text from end of line, to start of line

Post by mikeyww » 03 Mar 2022, 07:28

More of the same....

Code: Select all

str  = Celebration of July in Paris, van Gogh Vincent, canvas print
move = canvas print
MsgBox, 64, Output, % strMove(str, move)

strMove(str, move) {
 Return RegExReplace(str, "(.+) (" move ")$", "$2 $1")
}

siomosp
Posts: 27
Joined: 18 Jan 2022, 10:21

Re: Move matching text from end of line, to start of line

Post by siomosp » 03 Mar 2022, 16:25

BoBo wrote:
03 Mar 2022, 04:01
to move specific text
From where to receive the info on what text/words should be swapped?
The below script will take a string that has been copied to the clipboard (press Alt+c) and converts it as requested.
Output is copied to the clipboard.

Code: Select all

#SingleInstance, Force

;str := "Celebration of July in Paris, van Gogh Vincent, canvas print"
;str := "Blooming cherry, pink, white flowers wall stickers"


!c::												; press Alt+c
	Send ^c											; copy whatever text you've selected to the clipboard
	ClipWait										; wait till clipboard contains content
	if (clipboard="")
		Return
	StringLower, clipboard, str						; convert text to lower case
	arr := StrSplit(StrReplace(str,",",""),A_Space)	; remove commas, split at whatever space character
	url := arr[arr.Count()-1] "-" arr[arr.Count()]	; set the last two words to the front

	Loop % arr.Count()-2							; loop for array items (except the last two)
		url .= "-" arr[A_Index]						; add the remaining words
	Msgbox % clipboard := url						; copy converted str to clipboard
	Return
Thank you @BoBo
Yes my input is text from clipboard.
Your code works fine!
I will accept the next answer from @just me only because i can use the multiple search terms divided by pipe | , but i will use your part of code for converting text to lower case and remove commas

siomosp
Posts: 27
Joined: 18 Jan 2022, 10:21

Re: Move matching text from end of line, to start of line  Topic is solved

Post by siomosp » 03 Mar 2022, 16:27

just me wrote:
03 Mar 2022, 06:23

Code: Select all

#NoEnv
Terms := "canvas print|wall stickers|room divider" ; search terms divided by pipe |
Titles := ["Celebration of July in Paris, van Gogh Vincent, canvas print"
         , "Blooming cherry, pink, white flowers wall stickers"
         , "Any other arbitrary text."]
For Each, Title In Titles {
   Result := RegExReplace(Title, "i`a)(.*)(" . Terms . ")$", "$2 $1")
   MsgBox, %Title%`n->`n%Result%
}
ExitApp
?
Thank you @just me
Your script fits exactly my needs, especially because I can use the multiple search terms divided by pipe | !
Last edited by siomosp on 03 Mar 2022, 16:32, edited 2 times in total.

siomosp
Posts: 27
Joined: 18 Jan 2022, 10:21

Re: Move matching text from end of line, to start of line

Post by siomosp » 03 Mar 2022, 16:31

mikeyww wrote:
03 Mar 2022, 07:28
More of the same....

Code: Select all

str  = Celebration of July in Paris, van Gogh Vincent, canvas print
move = canvas print
MsgBox, 64, Output, % strMove(str, move)

strMove(str, move) {
 Return RegExReplace(str, "(.+) (" move ")$", "$2 $1")
}
Thank you @mikeyww
One more time your script is excellent , but i will use @just me mainly because I can use the multiple search terms divided by pipe |

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Move matching text from end of line, to start of line

Post by BoBo » 03 Mar 2022, 22:38

A good choice. I'd always go with just me's code as well :thumbup:
Btw, my code isn't using a static 'matchlist' as it uses the more dynamic filter criteria taking the last two words of a string by default.
So no need to "pre-configure" a list, the main reason why I asked, "From where to receive the info on what text/words should be swapped?".
Something, AFAICS mikeyww has pointed out before already. It's all about specs :mrgreen:
Happy scripting :)

Post Reply

Return to “Ask for Help (v1)”