Division into "," but not "/,"

Question How to write an expression to split a string into ',' but not '/,' ? Later I want to replace '/,' with ', ' .

More details ...

Separator : ','

Skip Char : '/'

Input Example : "Mister,Bill,is,made,of/,clay"

I want to split this entry into an array: {"Mister", "Bill", "is", "made", "of, clay"}

I know how to do this with char prev, cur; and some indexers, but this seems like a beta.

Java Regex has shared functionality, but I don't know how to replicate this behavior in C #.

Note. This is not a duplicate question, it is the same question, but for a different language.

+6
source share
5 answers

I believe you are looking for a negative lookbehind :

 var regex = new Regex("(?<!/),"); var result = regex.Split(str); 

this will split str into all commas that are not preceded by a slash. If you want to keep '/,' in a string, this will work for you.

Since you said that you want to split the line and a later one , replace '/,' with ', ' , you want to do it higher, then you can iterate over the result and replace the lines like this:

 var replacedResult = result.Select(s => s.Replace("/,", ", "); 
+8
source
 string s = "Mister,Bill,is,made,of/,clay"; var arr = s.Replace("/,"," ").Split(','); 

result: {"Mister", "Bill", "is", "made", "of clay"}

+3
source

Using Regex:

 var result = Regex.Split("Mister,Bill,is,made,of/,clay", "(?<=[^/]),"); 
+2
source

Just use Replace to remove the commas from your string:

  s.Replace("/,", "//").Split(',').Select(x => x.Replace("//", ",")); 
0
source

You can use this in C #

 string regex = @"(?:[^\/]),"; var match = Regex.Split("Mister,Bill,is,made,of/,clay", regex, RegexOptions.IgnoreCase); 

After that, you can replace /, and continue your work as you wish.

0
source

Source: https://habr.com/ru/post/976570/


All Articles