How to find matching words between two text strings

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
sn0365
Posts: 25
Joined: 03 Aug 2018, 12:59

How to find matching words between two text strings

19 Sep 2018, 05:59

Hello Everyone,

Can you please advise, how I can find any matching words between two variables containing text and putting them a new variable and deleting them from their initials variable. ?

Example -

Variable1 = Container A Slice 1 Inferior 1LN
Variable2 = Slice 1 Superior 1LN

---------after executing the code----------------------------

Output_Variable = Slice 1 1LN
Variable1 = Container A Inferior
Variable2 = Superior

regards,
sn0365
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: How to find matching words between two text strings

19 Sep 2018, 08:10

Hello, how about this

Code: Select all

Var1 := "Container A Slice 1 Inferior 1LN"
Var2 := "Slice 1 Superior 1LN"

Var1Array := StrSplit(Var1, " ")
Var2Array := StrSplit(Var2, " ")

Length2Loop := []
Length2Loop := (Var1Array.Length() > Var2Array.Length()) ? [Var1Array.Length(), Var2Array.Length()] : [Var2Array.Length(), Var1Array.Length()]

Loop, % Length2Loop[1]
{
	CheckOuter := (Var1Array.Length() > Var2Array.Length()) ? Var1Array[A_Index] : Var2Array[A_Index]
	Loop, % Length2Loop[2]
	{
		CheckInner := (Var1Array.Length() < Var2Array.Length()) ? Var1Array[A_Index] : Var2Array[A_Index]
		If (CheckOuter = CheckInner) {
			Var1 := RegExReplace(Var1, "\b" CheckInner "\b"), Var2 := RegExReplace(Var2, "\b" CheckInner "\b")
			Output_Var .= CheckInner " "
		}
	}
}
MsgBox % Trim(Var1) "`r`n" Trim(Var2) "`r`n" Trim(Output_Var)
Edit: Corrected so it no longer has the double 1
Last edited by MannyKSoSo on 19 Sep 2018, 09:00, edited 2 times in total.
Rohwedder
Posts: 7647
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How to find matching words between two text strings

19 Sep 2018, 08:22

Hallo,
or:

Code: Select all

V1 = Container A Slice 1 Inferior 1LN
V2 = Slice 1 Superior 1LN
V1 := StrReplace(V1," ",",")
V2 := StrReplace(V2," ",",")
Loop, Parse, V1, CSV
{
	If A_LoopField in %V2%
	{
		If A_LoopField  Not in %V3%
			V3 .= A_LoopField ","
	}
	Else
		V1a .= A_LoopField " "
}
Loop, Parse, V2, CSV
{
	If A_LoopField in %V1%
	{
		If A_LoopField  Not in %V3%
			V3 .= A_LoopField ","
	}
	Else
		V2a .= A_LoopField " "
}
V3 := Trim(StrReplace(V1,","," "))
V1 := Trim(V1a), V2 := Trim(V2a)
MsgBox, % V3
MsgBox, % V1
MsgBox, % V2

User avatar
TheDewd
Posts: 1513
Joined: 19 Dec 2013, 11:16
Location: USA

Re: How to find matching words between two text strings

19 Sep 2018, 08:25

Code: Select all

#SingleInstance, Force

Variable1 := "Container A Slice 1 Inferior 1LN"
Variable2 := "Slice 1 Superior 1LN"

Loop, Parse, Variable1, % A_Space
{
	Word1 := A_LoopField

	Loop, Parse, Variable2, % A_Space
	{
		Word2 := A_LoopField

		If (Word1 = Word2) {
			Output .= Word1 " "
			Variable1 := RegExReplace(Variable1, "\b" Word1 "\b")
			Variable2 := RegExReplace(Variable2, "\b" Word2 "\b")
		}
	}
}

MsgBox, % Output
MsgBox, % Variable1
MsgBox, % Variable2
sn0365
Posts: 25
Joined: 03 Aug 2018, 12:59

Re: How to find matching words between two text strings

19 Sep 2018, 08:42

Dear MannyKSoSo,

Thank you very much for your help.

May I please ask you to check the Output_Var

Based on the above example:

Output_Var = Slice 1 1 1LN ------- the middle 1 should not be there.
Final Var1 = Container A Inferior LN -------LN should not be there
Final Var2 = Superior LN-------LN should not be there

regards,

sn0365

MannyKSoSo wrote:Hello, how about this

Code: Select all

Var1 := "Container A Slice 1 Inferior 1LN"
Var2 := "Slice 1 Superior 1LN"

Var1Array := StrSplit(Var1, " ")
Var2Array := StrSplit(Var2, " ")

Length2Loop := []
Length2Loop := (Var1Array.Length() > Var2Array.Length()) ? [Var1Array.Length(), Var2Array.Length()] : [Var2Array.Length(), Var1Array.Length()]

Loop, % Length2Loop[1]
{
	CheckOuter := (Var1Array.Length() > Var2Array.Length()) ? Var1Array[A_Index] : Var2Array[A_Index]
	Loop, % Length2Loop[2]
	{
		CheckInner := (Var1Array.Length() < Var2Array.Length()) ? Var1Array[A_Index] : Var2Array[A_Index]
		If InStr(CheckOuter, CheckInner) {
			Var1 := StrReplace(Var1, CheckInner, ""), Var2 := StrReplace(Var2, CheckInner, "")
			Output_Var .= CheckInner " "
		}
	}
}
MsgBox % Var1 "`r`n" Var2 "`r`n" Output_Var
sn0365
Posts: 25
Joined: 03 Aug 2018, 12:59

Re: How to find matching words between two text strings

19 Sep 2018, 08:44

Dear Rohwedder,

Thank you very much for your help.

May I please ask if you can add the matching word as an additional output.

regards,
sn0365


Rohwedder wrote:Hallo,
or:

Code: Select all

V1 = Container A Slice 1 Inferior 1LN
V2 = Slice 1 Superior 1LN
V1 := StrReplace(V1," ",",")
V2 := StrReplace(V2," ",",")
Loop, Parse, V1, CSV
{
	If A_LoopField in %V2%
	{
		If A_LoopField  Not in %V3%
			V3 .= A_LoopField ","
	}
	Else
		V1a .= A_LoopField " "
}
Loop, Parse, V2, CSV
{
	If A_LoopField in %V1%
	{
		If A_LoopField  Not in %V3%
			V3 .= A_LoopField ","
	}
	Else
		V2a .= A_LoopField " "
}
V3 := Trim(StrReplace(V1,","," "))
V1 := Trim(V1a), V2 := Trim(V2a)
MsgBox, % V3
MsgBox, % V1
MsgBox, % V2

sn0365
Posts: 25
Joined: 03 Aug 2018, 12:59

Re: How to find matching words between two text strings

19 Sep 2018, 08:49

Dear TheDewd,

Thank you very much for your help.

Your code is working perfectly.

Can I please ask, if it is possible to only Pick the word "Slice" if it is followed by a number (example - Slice1) or (Slice 1)

thank you very much.

regards,
sn0365


TheDewd wrote:

Code: Select all

#SingleInstance, Force

Variable1 := "Container A Slice 1 Inferior 1LN"
Variable2 := "Slice 1 Superior 1LN"

Loop, Parse, Variable1, % A_Space
{
	Word1 := A_LoopField

	Loop, Parse, Variable2, % A_Space
	{
		Word2 := A_LoopField

		If (Word1 = Word2) {
			Output .= Word1 " "
			Variable1 := RegExReplace(Variable1, "\b" Word1 "\b")
			Variable2 := RegExReplace(Variable2, "\b" Word2 "\b")
		}
	}
}

MsgBox, % Output
MsgBox, % Variable1
MsgBox, % Variable2

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mikeyww and 282 guests