✅ 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:
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
Can you show what you've tried?
And the desired outcome?
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:
While I was testing for giving the example to you, I realized that probably there is something wrong with the regular expression
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?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
Asterisk is not a regex "keyword"
I thought it was from here
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:
This one is on C# specifically
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
You're absolutely right, I'm sorry. I fully blanked on that one somehow
Ero#1111
REPL Result: Success
Result: string[]
Compile: 491.413ms | Execution: 56.063ms | React with ❌ to remove this embed.
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
Ok, thanks. I was trying to understand what are all the characters I need to escape
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
Character Escapes in .NET Regular Expressions
Learn about special characters and escaped characters in .NET regular expressions.
Thank you for your help