C
C#

help

✅ Split string on list of strings and on whathever number

Aalkasel#15912/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:
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.
Eero12/15/2022
Can you show what you've tried? And the desired outcome?
Aalkasel#15912/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:
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
Eero12/15/2022
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?
Aalkasel#15912/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"
Aalkasel#15912/15/2022
I thought it was from here
Aalkasel#15912/15/2022
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:
Aalkasel#15912/15/2022
This one is on C# specifically
Aalkasel#15912/15/2022
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
Eero12/15/2022
You're absolutely right, I'm sorry. I fully blanked on that one somehow
MMODiX12/15/2022
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.
Eero12/15/2022
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
Aalkasel#15912/15/2022
Ok, thanks. I was trying to understand what are all the characters I need to escape
Aalkasel#15912/15/2022
Character Escapes in .NET Regular Expressions
Learn about special characters and escaped characters in .NET regular expressions.
Aalkasel#15912/15/2022
Thank you for your help

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
❔ How to populate my IGrouping with null values??So I have this way of grouping items, and I'm a little stuck. I have two classes, one is the child ❔ shorter way to export a list to a csv?Hey, i am searching for a quicker way to export a list to a csv with head than my solution under thi❔ Not able to read Entries of a zip fileHello everyone 🙂 I am trying to read a zip file in a dotnet Framework 4.7.2 Project using the cod❔ Use of unassigned local variableI'm getting an error saying "Use of unassigned local variable "Input"" I just followed a tutorial o❔ multi-line stringshi, if I have a string like ```@"this is a multi line string!"``` when my code is formatted, tabs g✅ Getting a string from a text file and get the whole line containing the stringI am making a app where I want to get the filename of a video file and then check if the filename ex❔ System.typeloadexception could not find method due to type load errorCode I am using: ```cs async void GamerMethod() { //Doing gamer things.❔ How do I search through a list for the first missing value in a range of numbers?For instance; a list of `0, 1, 2, 4, 5, 6, 7, 9` would return 3 if you're looking for the first "mis❔ Circle line-segment collision detectionTried following this https://stackoverflow.com/questions/1073336/circle-line-segment-collision-detec❔ Setting up VSCim trying to output some text from the terminal using Console.WriteLine() but the csc command is notEz LINQ HelpI am trying to get a list of `Value`s from the Cells where the Columns table `Number` is equal to 0.No route matches supplied values but values seem to match?Hi I am getting this error on return CreatedAtAction: ```c System.InvalidOperationException: No rout❔ Database Models and Tracking ReferenceI'm really new to EFCore and databases in general. I'm looking for a bit of guidance. I have 3 ta❔ how to get the current selected text from any window just like how clipboard does it?I want to simulate Ctrl + C without pressing Ctrl + C, basically able to read any selected text from✅ Sorting nestedSo I've got this data model that looks something like this (some parts of the fields have been omitt❔ Async Await On Logging - Waiting Response Without Await KeywordI have logging service where I call the logger.log async method without awaiting the response, expec❔ Validate Crypto addressI want to validate a cryptoaddress. The user can input an cryptoaddress... I want to validate this..✅ Creating a generic type based on a Type variable rather than a specific typeI've got the following code: ```cs public class QueryResponse<T> { T? Data { get; set; } bool Swhen an enemy hits the player it doesn't stop for some reason, and when the form loads I can't moveI'm not sure what I'm doing wrong when a bulletbill hits the bird it should stop and show up a messa❔ C Sharp Windows Form executable with local databaseHello guys, I've made a project in C# Windows Forms that uses local database (Azure). Since it's loc