Click here to Skip to main content
15,882,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi i want to get specific sub string from the string .. Explanation
if i have
string hex= " 7E 00 0F 10 01 00 13 A2 00 40 81 AB C4 FF FE 00 00 01 0B " ;

01 at end of string only from the string But ! i dont wanna make it with the normal substring function because in my case 01 may variate it can be 01 c5 or anything .. number of bites is not constant
in other way we can express it like
" 7E 00 0F 10 01 00 13 A2 00 40 81 AB C4 FF FE 00 00 (N) 0B "
While N is variable length ..but other packet parts is const length .
btw This is apacket and 01 or N is RF Data
Thanks
Posted
Updated 8-Jan-13 23:58pm
v2

1 solution

Use a Regex:
C#
string hex1 = " 7E 00 0F 10 01 00 13 A2 00 40 81 AB C4 FF FE 00 00 01 0B ";
string hex2 = " 7E 00 0F 10 01 00 13 A2 00 40 81 AB C4 FF FE 00 00 01 FF FE 66 0B ";
Regex reg = new Regex(@"(?<=7E 00 0F 10 01 00 13 A2 00 40 81 AB C4 FF FE 00 00 )(.. )*(?=0B )");
Match m = reg.Match(hex1);
if (m.Success)
    {
    Console.WriteLine(m.Value);
    }
m = reg.Match(hex2);
if (m.Success)
    {
    Console.WriteLine(m.Value);
    }
Will produce:
01 
01 FF FE 66 
 
Share this answer
 
Comments
promarkoo 9-Jan-13 7:21am    
data inside the string is not constant ... it is generated each calculation process . i was thinking if i can make like the first part of regex variable
like x=substring(0,20);
OriginalGriff 9-Jan-13 7:36am    
You can. Assuming your data starts with a 7E, you could discard the next 6 data pairs like this:
(?<=7E (.. ){6})(.. )*(?=0B )
And it would start capture with the A2 in your example.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900