c# - How to programmatically count the occurrence of regular expression as quickly as text editor -
i have written c# program opens particular directory. opens each file in directory , counts every occurrence of following regular expression @"^clm". program returns regular expression count each file , places count separate cell in spreadsheet. code using below:
list<string> linespost = system.io.file.readalllines(dipostfiles + curpostfile).tolist(); int y = 0; (int = linespost.count - 1; >= 0; i--) { string pattern = @"^clm"; match m = regex.match(linespost[i], pattern); while (m.success) { y++; break; } (xlrange.cells[startrow + x, 3] excel.range).value2 = y; }
this work, takes long time. if open given file in notepad++, example, , put in same regular expression hit count button, result quickly.
is there more efficient way count instances of regular expression? anticipating 5,000 occurrence per text file. overall size of each text file 5 mb.
any appreciated.
first , foremost, not need regex. checking if each line starts clm
.
instead of
string pattern = @"^clm"; match m = regex.match(linespost[i], pattern); while (m.success) { y++; break; }
you may use
if (linespost[i].startswith("clm")) y++;
if assign clm
variable, try assigning before loop if not change until loop end.
also, have line referring binding excel interop. suggest using late binding or dynamic
types work excel objects, , after loop. right now, access in loop, , might take lot of time. add list<string>
variable before loop, collect values, , insert excel after collected.
Comments
Post a Comment