✅ Split string on list of strings and on whathever number

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:
string value = "545: hello world: <<<example text 567 king's choice>>>";

string[] separators = new string[] {||
"<<<",
">>>",
":"
}

string[] substrings = value.Split(separators, StringSplitOptions.TrimEntries);
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).
Regex.Split Method (System.Text.RegularExpressions)
Splits an input string into an array of substrings at the positions defined by a regular expression match.
15 Replies
ero
ero2y ago
Can you show what you've tried? And the desired outcome?
alkasel#159
alkasel#1592y ago
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:
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);
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
[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
While I was testing for giving the example to you, I realized that probably there is something wrong with the regular expression
ero
ero2y ago
Very I'm not really sure if you're meaning to escape some stuff in your separators? Like what's system\\User? Or \*\*REF? \%s? What do you expect those to match?
alkasel#159
alkasel#1592y ago
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
ero
ero2y ago
Asterisk is not a regex "keyword"
alkasel#159
alkasel#1592y ago
I thought it was from here
alkasel#159
alkasel#1592y ago
Stack Overflow
How do I replace an actual asterisk character (*) in a Regex expres...
I have a statement: I have a string such as content = "* test " I want to search and replace it with so when I am done the string contains this: content = "() test (*)" My code is:
alkasel#159
alkasel#1592y ago
This one is on C# specifically
alkasel#159
alkasel#1592y ago
Stack Overflow
Find and replace Asterisk (*) from a string using Regex
I have a string like this: Durham * versus Yorkshire and I have used a regex in C# to replace that asterisk in the string with some text, as: string aString = "Durham * versus Yorkshire"; string
ero
ero2y ago
You're absolutely right, I'm sorry. I fully blanked on that one somehow
MODiX
MODiX2y ago
Ero#1111
REPL Result: Success
string[] separators =
{
@"\} SPACEFILL DP",
@"\} ZEROFILL DP",
@":",
@"'%s'",
@"%s'",
@"<<<<",
@"<<<",
@"<<",
@"<",
@">>>>",
@">>>",
@">>",
@">",
@"system\\User",
@"{S_NomeRicetta}",
@"\*\*REF",
@"\*\*",
@"/\*",
@"\*/",
@"____",
@"___",
@"\[",
@"\]",
@"\(",
@"\)",
@":",
@"-",
@"/",
@",",
@";",
@"\.",
@"\r\n"
};

string pattern = @"\d";
foreach (string separator in separators) {
pattern += $"|{separator}";
}

string value = "0: standby";
string[] symbols = Regex.Split(value, pattern);

symbols
string[] separators =
{
@"\} SPACEFILL DP",
@"\} ZEROFILL DP",
@":",
@"'%s'",
@"%s'",
@"<<<<",
@"<<<",
@"<<",
@"<",
@">>>>",
@">>>",
@">>",
@">",
@"system\\User",
@"{S_NomeRicetta}",
@"\*\*REF",
@"\*\*",
@"/\*",
@"\*/",
@"____",
@"___",
@"\[",
@"\]",
@"\(",
@"\)",
@":",
@"-",
@"/",
@",",
@";",
@"\.",
@"\r\n"
};

string pattern = @"\d";
foreach (string separator in separators) {
pattern += $"|{separator}";
}

string value = "0: standby";
string[] symbols = Regex.Split(value, pattern);

symbols
Result: string[]
[
"",
"",
" standby"
]
[
"",
"",
" standby"
]
Compile: 491.413ms | Execution: 56.063ms | React with ❌ to remove this embed.
ero
ero2y ago
But there are also other things you need to escape Like the period, which if not escaped, matches everything Which is what's caused your main issue Other things like parens, brackets, and braves need to be escaped too
alkasel#159
alkasel#1592y ago
Ok, thanks. I was trying to understand what are all the characters I need to escape
alkasel#159
alkasel#1592y ago
Character Escapes in .NET Regular Expressions
Learn about special characters and escaped characters in .NET regular expressions.
alkasel#159
alkasel#1592y ago
Thank you for your help
Want results from more Discord servers?
Add your server
More Posts