Jim Bob
Jim Bob

Reputation: 63

Need help writing regular expression

I want to grab anything after the FF's but not include the FF's.

[ 4xFF 82 DD E6 0F 59 EF 9B 0F 00 FF FF FF FF FF 82 A6 06 2C 81 FD 32 00 40 6B]

So I'd want the following two matches:

82 DD E6 0F 59 EF 9B 0F 00

82 A6 06 2C 81 FD 32 00 40 6B

I came up with this but it doesn't grab them in two matches.

I also think its a little long winded for a pattern.

(?!FF)((\d\d )|(\d[A-Z|a-z] )|([A-Z|a-z]\d )|([A-Z|a-z][A-Z|a-z] ))((\d\d )|(\d[A-Z|a-z])|([A-Z|a-z]\d)|([A-Z|a-z][A-Z|a-z]))

Upvotes: 0

Views: 124

Answers (3)

Sylwester Sys
Sylwester Sys

Reputation: 1

Or try my solution

   (?<=FF\s)([0-9A-E][0-9A-F]|[0-9A-F][0-9A-E]|\s?)+((?<=$)|(?<=\s))

Upvotes: 0

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112324

With this regex you will have to trim a trailing space from the results:

(([0-9A-E][0-9A-F]|[0-9A-F][0-9A-E])\s?)+

Explanation

((
  [0-9A-E][0-9A-F]      A hex digit excluding "F", followed by a hex digit
  |                     or
  [0-9A-F][0-9A-E]      A hex digit, followed by a hex digit excluding "F"
 )
 \s?                    an optional space
)+                      The whole pattern repeated at least once

This pattern describes sequences of 2-digit hex numbers excluding the number FF.

string pattern = @"(([0-9A-E][0-9A-F]|[0-9A-F][0-9A-E])\s?)+";
var matches = Regex.Matches(input, pattern);
foreach (var match in matches) {
    Console.WriteLine(match.Value.TrimEnd());
}

My previous solution returned the 2 matches you requested. If you simply need all the hex numbers one by one by skipping all the FF, you can search like this

string pattern = "[0-9A-E][0-9A-F]|[0-9A-F][0-9A-E]";
var matches = Regex.Matches(input, pattern);
foreach (var match in matches) {
    Console.WriteLine(match.Value);
}

Upvotes: 2

jdweng
jdweng

Reputation: 34421

Try following :

            string input = "[ 4xFF 82 DD E6 0F 59 EF 9B 0F 00 FF FF FF FF FF 82 A6 06 2C 81 FD 32 00 40 6B]";
            string pattern = @"(?'prefix'(\sFF)+)\s(?'capture'[^\]]+)";

            Match match = Regex.Match(input,pattern);

            Console.WriteLine(match.Groups["capture"].Value);
            Console.ReadLine();

Upvotes: 0

Related Questions