Regex.Split demonstrates strange behavior

I have a regex expression that I make to split on another line and I get weird results.

string subjectString = "Triage|Follow Up|QA"; string[] splitArray = null; try { splitArray = System.Text.RegularExpressions.Regex.Split(subjectString, @"(?<=(^|[^\\]))\|"); foreach (var item in splitArray) { System.Diagnostics.Debug.Print(item); } } catch { } 

Printable objects:

Sorting
e
Follow up
R
QA

Regular behavior works correctly in RegexBuddy, but not in C #. Any ideas on what causes the weird behavior? Additional points to explain why the split function works the way it is.

+4
source share
2 answers

Grouping (…) in your view statement causes this. Instead, try a non-exciting group:

 @"(?<=(?:^|[^\\]))\|" 

Or no additional grouping at all:

 @"(?<=^|[^\\])\|" 
+5
source

RegexBuddy has not yet emulated .NET behavior, including text matched by capturing groups in the array returned by Split() . To get the same behavior in .NET as in RegexBuddy, either change all your capture groups (...) to non-capture groups (?:...) , or use RegexOptions.ExplicitCapture to turn all unnamed groups into non-capture groups.

Including capture groups in the returned array, the .NET Split() function allows you to include both delimiters matching the regular expression and text between delimiters in the array. Separation using the regular expression <[^>]+> allows you to get text between HTML tags without HTML tags. Regular expression separation (<[^>]+>) allows you to get text between HTML tags, including HTML tags. (These simple regular expressions assume that the input consists of valid HTML with no HTML comments.)

+1
source

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


All Articles