.net - C# Pattern Search -



.net - C# Pattern Search -

my code below shows searching through file pattern .... there anyway possible add together code maybe search possible of 4 patterns.

so if doesn't match first 1 looks see if matches sec 1 , on ....

many thanks.

byte[] pattern = new byte[5] { 00, 00, 00, 08, 00 }; byte[] file = file.readallbytes("c:\\123.cfg"); var result = enumerable.range(0, file.length - pattern.length + 1) .where(i => pattern.select((b, j) => new { j, b }) .all(p => file[i + p.j] == p.b)) .select(i => + pattern.length - 1); int[] patternarray = result.toarray();

*********** edit ***********

while running programme inserted breakpoint see array storing .... reported

response count = 6 system.collections.generic.list<int[]> [0] {int[1]} int[] [0] 1577 int [1] {int[0]} int[] [2] {int[0]} int[] [3] {int[0]} int[] [4] {int[0]} int[] [5] {int[6]} int[] [0] 31 int [1] 246 int [2] 448 int [3] 663 int [4] 864 int [5] 1734 int

the pattern results seem there, right thinking two-dimensional array? ... if there anyway have in single array?

many thanks

create array (or list...or other enumerable) of byte[] , 'matched' boolean , while loop around pattern search this:

list<byte[]> patterns = new list<byte[]>() { new byte[] { 00, 00, 00, 08, 00 }, new byte[] { 00, 00, 00, 08, 01 }, new byte[] { 00, 00, 00, 08, 02 }, new byte[] { 00, 00, 00, 08, 03 } }; //bool matched = false; foreach (byte[] pattern in patterns) { //while (!matched) //{ byte[] file = file.readallbytes("c:\\123.cfg"); var result = enumerable.range(0, file.length - pattern.length + 1) .where(i => pattern.select((b, j) => new { j, b }) .all(p => file[i + p.j] == p.b)) .select(i => + pattern.length - 1); int[] patternarray = result.toarray(); // if (result != null) // matched = true; //} }

[edit] function returns of valid matches list:

public list<int[]> dopatternsearch() { list<byte[]> patterns = new list<byte[]>() { new byte[] { 00, 00, 00, 08, 00 }, new byte[] { 00, 00, 00, 08, 01 }, new byte[] { 00, 00, 00, 08, 02 }, new byte[] { 00, 00, 00, 08, 03 } }; //this becomes container hold of valid results list<int[]> response = new list<int[]>(); foreach (byte[] pattern in patterns) { byte[] file = file.readallbytes("c:\\123.cfg"); var result = enumerable.range(0, file.length - pattern.length + 1) .where(i => pattern.select((b, j) => new { j, b }) .all(p => file[i + p.j] == p.b)) .select(i => + pattern.length - 1); if (result != null) { //if result not null add together list. response.add(result.toarray<int>()); } } homecoming response; }

c# .net linq

Comments

Popular posts from this blog

layout - css: mystery "margin" -

c# - Mysql Syntax Error -