C
Join ServerC#
help
✅ Split string on list of strings and on whathever number
Aalkasel12/15/2022
Hi,
I need to split a string in substrings, splitting every time I find some specific strings (e.g. ">>>") and everytime I find a number of whathever length. I can successfully split on any of the specified string, but I cannot do it with numbers. My C# code is the following:
I've tried with Regex.split as well but it includes all kind of characters, so I think it's not supposed to be used like that (https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.split?view=net-7.0).
I need to split a string in substrings, splitting every time I find some specific strings (e.g. ">>>") and everytime I find a number of whathever length. I can successfully split on any of the specified string, but I cannot do it with numbers. My C# code is the following:
string value = "545: hello world: <<<example text 567 king's choice>>>";
string[] separators = new string[] {||
"<<<",
">>>",
":"
}
string[] substrings = value.Split(separators, StringSplitOptions.TrimEntries);
I've tried with Regex.split as well but it includes all kind of characters, so I think it's not supposed to be used like that (https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.split?view=net-7.0).
EEro12/15/2022
Can you show what you've tried?
EEro12/15/2022
And the desired outcome?
Aalkasel12/15/2022
Ok. Let's assume I have the string "0: standby" (it is generated by an external program and I need to translate only the relevant parts of it).
I'm trying the following approach:
The result I expect is an array containing the string " standby" and probably some empty string too (e.g. between 0 and :). Instead, what I got is the following array:
I'm trying the following approach:
string[] separators = new string[] {
@"} SPACEFILL DP",
@"} ZEROFILL DP",
@":",
@"'\%s'",
@"\%s'",
@"<<<<",
@"<<<",
@"<<",
@"<",
@">>>>",
@">>>",
@">>",
@">",
@"system\\User",
@"{S_NomeRicetta}",
@"\*\*REF",
@"\*\*",
@"/\*",
@"\*/",
@"____",
@"___",
@"[",
@"]",
@"(",
@")",
@":",
@"-",
@"/",
@",",
@";",
@".",
@"\\r\\n"
};
string regEx = "\\d";
foreach (string separator in separators) {
regEx = string.Concat(regEx, "|", separator);
}
string value = "0: standby";
string[] symbols = Regex.Split(value, regEx);
The result I expect is an array containing the string " standby" and probably some empty string too (e.g. between 0 and :). Instead, what I got is the following array:
[0] "" string
[1] "" string
[2] "" string
[3] "" string
[4] "" string
[5] "" string
[6] "" string
[7] "" string
[8] "S" string
[9] "" string
[10] "t" string
[11] "" string
[12] "a" string
[13] "" string
[14] "n" string
[15] "" string
[16] "d" string
[17] "" string
[18] "b" string
[19] "" string
[20] "y" string
[21] "" string
[22] "" string
Aalkasel12/15/2022
While I was testing for giving the example to you, I realized that probably there is something wrong with the regular expression
EEro12/15/2022
Very
EEro12/15/2022
I'm not really sure if you're meaning to escape some stuff in your separators?
EEro12/15/2022
Like what's
system\\User
?EEro12/15/2022
Or
\*\*REF
? \%s
?EEro12/15/2022
What do you expect those to match?
Aalkasel12/15/2022
For **REF, since asterisk is a regex keyword, I need to escape it in order to let regex know that I want to match exactly **REF
EEro12/15/2022
Asterisk is not a regex "keyword"
Aalkasel12/15/2022
I thought it was from here
Aalkasel12/15/2022
This one is on C# specifically
EEro12/15/2022
You're absolutely right, I'm sorry. I fully blanked on that one somehow
EEro12/15/2022
But there are also other things you need to escape
EEro12/15/2022
Like the period, which if not escaped, matches everything
EEro12/15/2022
Which is what's caused your main issue
EEro12/15/2022
Other things like parens, brackets, and braves need to be escaped too
Aalkasel12/15/2022
Ok, thanks. I was trying to understand what are all the characters I need to escape
Aalkasel12/15/2022
I was able to succeed by using this list too
https://learn.microsoft.com/en-us/dotnet/standard/base-types/character-escapes-in-regular-expressions
https://learn.microsoft.com/en-us/dotnet/standard/base-types/character-escapes-in-regular-expressions
Aalkasel12/15/2022
Thank you for your help