Reputation: 303
This is the string that I want to match:
"t=0, data=00 00 00 f1 00 00 00 00".
The following works:
(@"^(t)=[0-9]+,((\s[0-9A-F-a-f]{2}){8})")
matches:
"t=0, 00 00 00 f1 00 00 00 00"
(@"^(t)=[0-9]+,\s\w+=")
matches:
"t=0, data="
The following doesn't work:
(@"^(t)=[0-9]+,\s\w+=((\s[0-9A-F-a-f]{2}){8})")
doesn't match:
"t=0, data=00 00 00 f1 00 00 00 00"
Here is my code at the moment :
Regex rgx = new Regex(@"^(t)=[0-9]+,\s\w+=((\s[0-9A-F-a-f]{2}){8})");
if(rgx.IsMatch(line.Trim())){
//...
}
Upvotes: 1
Views: 207
Reputation: 1949
^(t)=[0-9]+,\s\w+=([0-9A-Fa-f]{2}(\s|$)){8}
will do the trick.
you placed a whitespace character after each two digits, but because the last two digits don't have a whitespace after them the regex won't match. Now the regex engine can choose between a whitespace character or an end of string.
Edit: changed word boundary to end of string
Edit2: also, take a look at this: http://www.regular-expressions.info/anchors.html
Upvotes: 1
Reputation: 61178
Okay, to match the pattern:
t=0, data=00 00 00 f1 00 00 00 00
I have made some assumptions:
t=
data=
Then this pattern will work:
^t=[0-9]++,\s*+data=(?<data>(?:[0-9a-f]{2}\s?){8})$
A test in Java
public static void main(String[] args) throws SQLException {
final String data = "t=0, data=00 00 00 f1 00 00 00 00";
final Pattern pattern = Pattern.compile("^t=[0-9]++,\\s*+data=(?<data>(?:[0-9a-fA-F]{2}\\s?){8})$");
final Matcher matcher = pattern.matcher(data);
if (matcher.matches()) {
System.out.println(matcher.group());
System.out.println(matcher.group("data"));
}
}
Output:
t=0, data=00 00 00 f1 00 00 00 00
00 00 00 f1 00 00 00 00
Upvotes: 0
Reputation: 328
for (\s[0-9A-F-a-f]{2}) to match first pair of digits just after = sign you need to have a space between the = sign and first digit. But it seems you dont.
Also is (\s[0-9A-F-a-f]{2}) correct?. I think you need to delete the '-' between F and a.
Upvotes: 0
Reputation: 8420
If I understand well your needs you could use the following regex :
Regex rgx = new Regex(@"(^t=[0-9]+,[a-z ]+=([A-Fa-f0-9]{2} ?){8})");
if(rgx.IsMatch(line.Trim())){
//...
}
So:
^t=[0-9]+,[a-z ]+=
matches everything before the hex numbers. ([A-Fa-f0-9]{2} ?){8}
matches 8 groups of 2 hex character followed or not by a space.Everything is included in the $1
variable as the enclosing parenthesis matches the full line.
Upvotes: 1