Find pattern in string with multiple unknown hex values and obtain them

0

String: "Some text This is first hex number 0x00000001, This is second hex number 0x00000002 Some text" Pattern: "This is first hex number 0x00000001, This is second hex number 0x00000002" Expected output: "0x00000001" and "0x00000002" (string or int, doesn't matter)

In C# I want to search a String for known Pattern (as in Pattern, hex values may be different, but always in format 0xXXXXXXXX) and if it is found obtain both hex values as string or int.

I am somehow fimiliar with Regex and have one solution, but it seems very 'dirty'. I am almost sure that there should be simplier solution.

string input = "Some text This is first hex number 0x00000001, This is 
second hex number 0x00000002 Some text";
var pattern = new Regex(@"This is first hex number 0x[0-9A-F]{8}, This is second hex number 0x[0-9A-F]{8}");

if (pattern.Match(input).Success)
{
    Console.WriteLine("Patter found!");
    Console.WriteLine("First Hex: {0}", pattern.Match(input).ToString().Split(' ')[6].Trim(','));
    Console.WriteLine("Second Hex: {0}", pattern.Match(input).ToString().Split(' ')[10]);
}
c#
regex
asked on Stack Overflow Mar 25, 2020 by user1762979 • edited Mar 25, 2020 by user1762979

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0