joe2
(Joe)
April 26, 2023, 10:15pm
1
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
jack.chan
(Jack Chan)
April 26, 2023, 10:29pm
2
@joe2 use except function
for it to work both ways you can do this:
str1.tolist.except(str2.tolist).tolist.concat(str2.tolist.except(str1.tolist).tolist).tolist
Yoichi
(Yoichi)
April 26, 2023, 11:40pm
4
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?
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,
joe2
(Joe)
April 26, 2023, 11:54pm
5
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
Yoichi
(Yoichi)
April 27, 2023, 12:06am
6
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,
joe2
(Joe)
April 27, 2023, 3:05am
7
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
Yoichi
(Yoichi)
April 27, 2023, 3:55am
8
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
system
(system)
Closed
May 1, 2023, 10:09pm
10
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.