Compare two string and finding the difference

Hi

I have two string which contains names,
I need to find the difference and return the value

I am trying through regex operation
str1- Rocky Thomas
str2- Rocky Tomas

here the output is h
I have used finding matching pattern matches with pattern as - “[(?<^”+str2+“]+”
with input as str1

this work fines in this situation but in another case like
str1- John Anthony
str2- john Antony

output should b -h but I get empty as output

@joe2 use except function

image

for it to work both ways you can do this:

str1.tolist.except(str2.tolist).tolist.concat(str2.tolist.except(str1.tolist).tolist).tolist

Hi,

this work fines in this situation but in another case like
str1- John Anthony
str2- john Antony

output should b -h but I get empty as output

Because your pattern : [(?<^”+str2+“] matches characters which exists inside square brackets, and both string contains “h”

And also I don’t think this pattern works for your requirement even if the first example. Can you share your actual expression using screenshot etc?

image

If you want to compare them word for word, can you try the following?

String.Join(vbcrlf,str1.Split({" "c}).zip(str2.Split({" "c}),Function(s1,s2) System.Text.RegularExpressions.Regex.Matches(s1,"(?i)[^"+System.Text.RegularExpressions.Regex.Escape(s2)+"]")).SelectMany(Function(mc) mc.Cast(Of System.Text.RegularExpressions.Match).Select(Function(m) m.Value)))

Sample20230427-1L.zip (2.3 KB)

Regards,

I want to compare them letter by letter and return the difference.

yes you are correct, It is working fine when larger string is kept as input and smaller string is set as pattern. But in this case john Antony and John Anathony, “h” is already present in first name, hence it is returning empty value

Hi,

Sorry, but I didn’t say enough. The above expression extract each word in both string then compare them letter by letter. (It assumes number of word is same in both string) Does it work for you?
If not, can you share more examples?

Regards,

Unfortunately it didn’t work, some example are as follows:

Parker Rager
Parker Rage

erin hannah
erin hanah

here, the letter ‘r’ is the missed value, but it empty value

Hi,

in this case, perhaps you should use diff library instead of regex.

Can you try the following sample?

Sample20230427-1Lv2.zip (2.8 KB)

As this sample uses Diffplex library, please add nuget.org as feed in settings of MangePackage.

https://api.nuget.org/v3/index.json

Regards,

2 Likes

Thank you it worked

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.