Wednesday, 15 February 2012

c# - Find Matches using Regex in File Lines -



c# - Find Matches using Regex in File Lines -

i reading list of files directory , looking patterns:

a. [[[something]]] > string "something" b. [[[something///comment]]] > strings "something" , "comment" c. [[[enter between %0 , %1 characters|||val 1|||val 2]]] >> string before first ||| "enter between %0 , %1 characters"

so tried following:

ilist<string> files = directory.getfiles(path, "*.cshtml", searchoption.alldirectories).tolist(); idictionary<string, tuple<int32, string>> items = new dictionary<string, tuple<int32, string>>(); regex regex = new regex(@"\[\[\[.*\]\]\]"); foreach (string file in files) { foreach (string line in file.readalllines(file)) { matchcollection matches = regex.matches(line); foreach (match match in matches) { if (match != null) { items.add(match.value, new tuple<int32, string>(number, file)); } } } }

note: using readalllines because need line number of each match find.

could have help in following:

when using regex @"[[[.*]]]" found situation not work:

viewinfo.title("[[[title]]]").description("[[[description]]]");

i title]]]").description("[[[description]]]

i have not been able apply rules (b) , (c).

is possible improve performance or code ok?

you need ungreedy expression: .*? seek consume as few characters possible .

try this: @"\[\[\[(?:(.*?)\|\|\|.*?|(.*?)///(.*?)|(.*?))\]\]\]" (it of import set longest possible alternatives first or .*? eat whole string)

use file.readlines along variable you'll increment @ each iteration counting lines. way won't have hold whole file in memory.

c#

No comments:

Post a Comment