C
C#2y 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
Anton2y ago
No built-in solution, but I think the straightforward solution is optimal well maybe not optimal actually
remmy_clarke_jr
@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
SWEETPONYOP2y ago
unknown strings oh thanks for the idea
remmy_clarke_jr
Can you define some constraints?
SWEETPONY
SWEETPONYOP2y ago
yes
remmy_clarke_jr
If you can't you're in the area of NLP and machine learning lol
SWEETPONY
SWEETPONYOP2y ago
I thought it will be easy
remmy_clarke_jr
You need to define what a word is Why? "findthefindthemostcommonword" How would you find the most common word in this string?
SWEETPONY
SWEETPONYOP2y ago
it is easy to find 1 common word in string but it's harder to find some common in string
remmy_clarke_jr
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
SWEETPONYOP2y ago
find
remmy_clarke_jr
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
SWEETPONYOP2y ago
using regex?
remmy_clarke_jr
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
SWEETPONYOP2y ago
ok, I will try
remmy_clarke_jr
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
SWEETPONYOP2y 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
Is there a token you can split by that is consistently present in the string? seems like / and : are
SWEETPONY
SWEETPONYOP2y ago
yes, it is /
remmy_clarke_jr
Try splitting by that that will give you an array Then just count how many times you find N item in the array
SWEETPONY
SWEETPONYOP2y 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
Pobiega2y 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
SWEETPONYOP2y 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
Pobiega2y ago
you could probably tweak it a bit based on patterns, but finding "common paths" in entirely unknown strings is hard
Want results from more Discord servers?
Add your server