简体   繁体   English

在正则表达式中匹配字符串

[英]Matching a string in regex

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-Faf]{2}){8})") matches: "t=0, 00 00 00 f1 00 00 00 00" (@"^(t)=[0-9]+,((\\s[0-9A-Faf]{2}){8})")匹配: "t=0, 00 00 00 f1 00 00 00 00"

  • (@"^(t)=[0-9]+,\\s\\w+=") matches: "t=0, data=" (@"^(t)=[0-9]+,\\s\\w+=")匹配: "t=0, data="

The following doesn't work: 以下内容不起作用

  • (@"^(t)=[0-9]+,\\s\\w+=((\\s[0-9A-Faf]{2}){8})") doesn't match: "t=0, data=00 00 00 f1 00 00 00 00" (@"^(t)=[0-9]+,\\s\\w+=((\\s[0-9A-Faf]{2}){8})")不匹配: "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())){
  //...
}

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]+,[az ]+= matches everything before the hex numbers. ^t=[0-9]+,[az ]+=匹配十六进制数字之前的所有内容。
  • ([A-Fa-f0-9]{2} ?){8} matches 8 groups of 2 hex character followed or not by a space. ([A-Fa-f0-9]{2} ?){8}匹配8组2个十六进制字符,后跟一个空格。

Everything is included in the $1 variable as the enclosing parenthesis matches the full line. $1变量中包含所有内容,因为括号括起了整行。

^(t)=[0-9]+,\\s\\w+=([0-9A-Fa-f]{2}(\\s|$)){8} will do the trick. ^(t)=[0-9]+,\\s\\w+=([0-9A-Fa-f]{2}(\\s|$)){8}可以解决问题。 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 Edit2:另外,看看这个: http : //www.regular-expressions.info/anchors.html

for (\\s[0-9A-Faf]{2}) to match first pair of digits just after = sign you need to have a space between the = sign and first digit. 为(\\ s [0-9A-Faf] {2})匹配=符号后的第一对数字,您需要在=符号和第一位数字之间留一个空格。 But it seems you dont. 但似乎你没有。

Also is (\\s[0-9A-Faf]{2}) correct?. 另外(\\ s [0-9A-Faf] {2})是否正确? I think you need to delete the '-' between F and a. 我认为您需要删除F和a之间的“-”。

Okay, to match the pattern: 好的,要匹配模式:

t=0, data=00 00 00 f1 00 00 00 00

I have made some assumptions: 我做了一些假设:

  • there can be any number of digits following t= t=之后可以有任意数量的数字
  • there are 8 groups of two hex digits following the data= data=后面有8组,每组两个十六进制数字
  • you need the data in a match group 您需要匹配组中的数据

Then this pattern will work: 然后此模式将起作用:

^t=[0-9]++,\s*+data=(?<data>(?:[0-9a-f]{2}\s?){8})$

A test in Java 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM