Hi i was trying to filter a large of text file using Regex.Split with about more than 20 of string patterns i need to use but the outcome is not correct and there are so many empty strings.
below is my code
string[] tmp = Regex.Split(originalString, @"(LINE|3DFACE|3DSOLID|ARC|ATTDEF|ATTRIB|BODY|CIRCLE|DIMENSION|ELLIPSE|HATCH|HELIX|IMAGE|INSERT|LEADER|LIGHT||LWPOLYLINE|MLINE|MLEADER|MLEADERSTYLE|MTEXT|OLEFRAME|OLE2FRAME|POINT|POLYLINE|RAY|REGION|SECTION|SEQEND|SHAPE|SOLID|SPLINE|SUN|SURFACE|TABLE|TEXT|TOLERANCE|TRACE|UNDERLAY|VERTEX|VIEWPORT|WIPEOUT|XLINE)");
if i only use about 5 of them then the outcome is able to do exactly as i need, is there any limit for regex.split for string patterns?
EDIT 1
THANKS TO @MRAB
here is the code for actually running working and right output
string fileName = textBox1.Text;
var stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
TextReader sr = new StreamReader(fileName);
string originalString = sr.ReadToEnd();
sr.Close();
string[] tmp = Regex.Split(originalString, @"(3DFACE|3DSOLID|ACAD_PROXY_ENTITIY|ARC|ATTDEF|ATTRIB|BODY|CIRCLE|DIMENSION|ELLIPSE|HATCH|HELIX|IMAGE|INSERT|LEADER|LIGHT|LWPOLYLINE|MLINE|MLEADERSTYLE|MLEADER|MTEXT|OLEFRAME|OLE2FRAME|POINT|POLYLINE|RAY|REGION|SEQEND|SHAPE|SOLID|SPLINE|SUN|SURFACE|TABLE|TEXT|TOLERANCE|TRACE|UNDERLAY|VERTEX|VIEWPORT|WIPEOUT|XLINE|LINE)");
List<string> result = new List<string>();
for (var i = 1; i < tmp.Count() - 1; i += 2)
{
result.Add(tmp[i] + tmp[i + 1]);
}
You’re using the static
Splitmethod of theRegexclass. There’s also aSplitmethod onRegexinstances, for example:Your code contains this in the regex:
which looks like a mistake to me.
You should also note that when you have a regex like this:
and a string like this:
it will match
"MLEADER"because that occurs earlier in a regex. Where a short literal occurs as a prefix of a longer literal, put the longer one earlier.