C
C#17mo ago
SWEETPONY

✅ how to find some common strings in array?

I have following strings:
astra-srv:658712/sys/broker/state/astra-srv:658712_svc_core
astra-srv:658712/sys/broker/state/astra-srv:658712_svc_client_notify
astra-srv:658712/sys/broker/state/astra-srv:658712_svc_mailer
astra-srv:658712/svc
astra-srv:658712/sys/broker/state/astra-srv:658712_svc_core
astra-srv:658712/sys/broker/state/astra-srv:658712_svc_client_notify
astra-srv:658712/sys/broker/state/astra-srv:658712_svc_mailer
astra-srv:658712/svc
I'd like to return ["astra-srv:658712/sys/broker/state", "astra-srv:658712/svc"] I think I can't even group these strings
24 Replies
Anton
Anton17mo ago
No built-in solution, but I think the straightforward solution is optimal well maybe not optimal actually
remmy_clarke_jr
remmy_clarke_jr17mo ago
@penance Are you trying to find a known or unknown string? You need to define a lot of parameters to solve this problem If you know ahead of time what your looking for and you're trying to extract "Frequency" as a feature Regex easy super easy If you're trying to find the frequency of an unknown word You're need to define some constraints to start searching and grouping within
SWEETPONY
SWEETPONY17mo ago
unknown strings oh thanks for the idea
remmy_clarke_jr
remmy_clarke_jr17mo ago
Can you define some constraints?
SWEETPONY
SWEETPONY17mo ago
yes
remmy_clarke_jr
remmy_clarke_jr17mo ago
If you can't you're in the area of NLP and machine learning lol
SWEETPONY
SWEETPONY17mo ago
I thought it will be easy
remmy_clarke_jr
remmy_clarke_jr17mo ago
You need to define what a word is Why? "findthefindthemostcommonword" How would you find the most common word in this string?
SWEETPONY
SWEETPONY17mo ago
it is easy to find 1 common word in string but it's harder to find some common in string
remmy_clarke_jr
remmy_clarke_jr17mo ago
By doing a look ahead? You need to extract A feature from the string to define what a word even is Define for me the word boundry of the string I just showed you There are a few ways to do this depending on your exact requirements If you have a non-word character you can split by Happy days
SWEETPONY
SWEETPONY17mo ago
find
remmy_clarke_jr
remmy_clarke_jr17mo ago
find? Huh? That is still doing some kind of look ahead I don't know what method you're specifically talking about (A LINQ method?) But given a specific input Yes Its very easy
SWEETPONY
SWEETPONY17mo ago
using regex?
remmy_clarke_jr
remmy_clarke_jr17mo ago
I just keep looking ahead until Str[n] == Token And then scan until the end of the boundry Sure You can use RegEx for that
SWEETPONY
SWEETPONY17mo ago
ok, I will try
remmy_clarke_jr
remmy_clarke_jr17mo ago
But if your token is unknown you can not @penance If you join dev-vc-0 I can better explain your problem to you I don't think RegEx will work for you
SWEETPONY
SWEETPONY17mo ago
I'm sorry but unfortunately, I won't be able to do it now I will try to use all my leet code power to solve this problem
remmy_clarke_jr
remmy_clarke_jr17mo ago
Is there a token you can split by that is consistently present in the string? seems like / and : are
SWEETPONY
SWEETPONY17mo ago
yes, it is /
remmy_clarke_jr
remmy_clarke_jr17mo ago
Try splitting by that that will give you an array Then just count how many times you find N item in the array
SWEETPONY
SWEETPONY17mo ago
hm okey but my problem will not be solved any way
astra-srv:658712/sys/broker/state/astra-srv:658712_svc_core
astra-srv:658712/sys/broker/state/astra-srv:658712_svc_client_notify
astra-srv:658712/sys/broker/state/astra-srv:658712_svc_mailer
astra-srv:658712/svc
astra-srv:658712/sys/broker/state/astra-srv:658712_svc_core
astra-srv:658712/sys/broker/state/astra-srv:658712_svc_client_notify
astra-srv:658712/sys/broker/state/astra-srv:658712_svc_mailer
astra-srv:658712/svc
-> ["astra-srv:658712/sys/broker/state", "astra-srv:658712/svc"]
Pobiega
Pobiega17mo ago
looks like you jsut want to find the roots?
private static IEnumerable<string> FindRoots(IEnumerable<string> strings)
{
return strings
.Select(str => str.Split('/')[..2])
.Select(split => string.Join('/', split))
.Distinct();
}
private static IEnumerable<string> FindRoots(IEnumerable<string> strings)
{
return strings
.Select(str => str.Split('/')[..2])
.Select(split => string.Join('/', split))
.Distinct();
}
this should work.
SWEETPONY
SWEETPONY17mo ago
yes, it works, thanks but I needed to return common paths, not only root astra-srv:658712/sys/broker/state/ not astra-srv:658712/sys anyway thanks for your help
Pobiega
Pobiega17mo ago
you could probably tweak it a bit based on patterns, but finding "common paths" in entirely unknown strings is hard