Comparing strings to find a match

I have two strings address “Baler Moset Archi,21/22 MacCurtain Street,Farnham House” & “Baler Moset Archi,Farnham House,21/22 MacCurtain Street” the text within them are the same but just in different positions. is there a way to Compare these two string and flag it as a match even though certain parts of the address swapped around and also flag it as a match but the order is different?

1 Like

Hi

Let’s do it simply

Say you have two string variable and saved these two strings

Str1 and str2

Now use a IF condition like this

Str1.ToString.Trim.ToUpper.Equals(str2.ToString.Trim.ToUpper)

If yes then they are same and goes to THEN block where use a assign activity to mark the flag

Str_flag1 = “match even though certain parts of the address swapped around”

If false goes to ELSE block where use a assign activity

Str_flag2 = “match but the order is different”

Hope this helps
Cheers @duaine.b

That isn’t going to find a match if the address parts are moved around. And you can’t just assume they match if the strings aren’t identical.

@duaine.b What you’ll need to do is split the first string on the comma and this will give you an array, then you can check each element in the array and see if it exists in the second string. You can use an “all match” boolean flag to detect.

  • Assign allMatch = true
  • For Each in Split(str1,“,”)
    ** If NOT str2.ToUpper.Contains(CurrentItem.ToUpper)
    *** Assign allMatch = false
    *** break

What will happen is if it loops through and all the items from str exist in str2, nothing happens and allMatch remains true. If it finds an element in str1 that doesn’t exist in str2, it sets allMatch to false and breaks out of the loop because there is no point in continuing to check. The ToUpper is because I assume you’d want a match to be found even if the case is different.

2 Likes

Hi @duaine.b,

In your case you can use “Levenshtein” match. It will guve you match percent based on two strings. You can set flag accoording to your match percent output.

You can find this activity under-

You can try this as well-

Regards,
Dev

Hi, check this package: FuzzyWuzzy - RPA Component | UiPath Marketplace | Overview
Find ‘Score’ activity and select Token Set Ration in properties (score algorithm).

This setup returns score from 0 to 100 based on how good is match, with word order tolerance. So in your case it returns 100 as expected (because words are the same and we have order tolerance):

You can use output to create threshold if we want to match with tolerance to small mistakes or shortcuts (its very often when comparing addresses)

1 Like

Oh and if you want to be able to detect that they match but the order is different, then you’ll need an additional flag and steps:

  • Assign sameOrder = true
  • Assign allMatch = true
  • For Each in Split(str1,“,”) (set index variable)
    ** If NOT str2.ToUpper.Contains(CurrentItem.ToUpper)
    *** Assign allMatch = false
    ** Else
    *** If NOT currentItem.ToUpper = Split(str2,“,”)(index -1).ToUpper
    **** Assign sameOrder = false
    *** break

The additional If checks if the current value (from the loop of str1 items) is in the same position in str2. If it is not, then set sameOrder to false.

Hi @duaine.b

you can try this way so,
it will match the two strings and if it matches then it will give you output as matched if not it will give not matched

str_Text1 =“Baler Moset Archi,21/22 MacCurtain Street,Farnham House”

str_Text2= “Baler Moset Archi,Farnham House,21/22 MacCurtain Street”

Result = If(str_Text2.ToLower.Contains(str_Text1.Split(",“c)(0).ToLower) And str_Text2.ToLower.Contains(str_Text1.Split(”,“c)(1).ToLower) And str_Text2.ToLower.Contains(str_Text1.Split(”,"c)(2).ToLower),“Matched”,“Not Matched”)