C#拆分并从不一致的字符串中获取值

C#拆分并从不一致的字符串中获取值,c#,regex,C#,Regex,在C#编码中,我必须根据十六进制值拆分字符串列表,这很复杂。下面是字符串列表 1/2JHT(0x2) 3/4JHT(0x4) 7/8JHT(0x8) 1/2JHT-B(0x10) 3/4JHT-B(0x20) (126;112)RS(0x80) (194;178)卢比(0x100) (208;192)RS(0x200) 2/3RET(0x1000) 3/4RET(0x2000) 7/8RET(0x4000) 1/2FAST_1024(0x8000) 1/2FAST_4096(0x10000) 1

在C#编码中,我必须根据十六进制值拆分字符串列表,这很复杂。下面是字符串列表

  • 1/2JHT(0x2)
  • 3/4JHT(0x4)
  • 7/8JHT(0x8)
  • 1/2JHT-B(0x10)
  • 3/4JHT-B(0x20)
  • (126;112)RS(0x80)
  • (194;178)卢比(0x100)
  • (208;192)RS(0x200)
  • 2/3RET(0x1000)
  • 3/4RET(0x2000)
  • 7/8RET(0x4000)
  • 1/2FAST_1024(0x8000)
  • 1/2FAST_4096(0x10000)
  • 1/2FAST_16384(0x20000)

  • 例如,如果我得到十六进制值20000,正确的对应值是no14,它是1/2FAST\u 16384。因此,我需要将其分为3个不同的值,即a)1/2b)FAST和c)16384。也许有人可以给出一些关于如何实现它的想法,因为字符串的长度和模式是不一致的。也不确定是否可以使用正则表达式来解决此问题。

    您可以使用此正则表达式:

    (?<a>(\(\d+;\d+\))|(\d+\/\d+))(?<b>[^\(_]+)(_(?<c>[^\(]+))?
    

    (?

    您可以将您的价值观分为三组:

    解释

    ^ # Assert position at the beginning of the string. ( # Capture in a group (group 1) \(? # Match optional opening parenthesis \d+ # match one or more digits [/;] # Match forward slash or semicolon \d+ # Match One or more digits \)? # Match optional closing parenthesis ) # Close captured group ( # Capture in a group (group 2) [A-Z-]+ # Match an uppercase character or a hyphen one or more times ) # Close captured group (?: # A non capturing group _ # Match an underscore (\d+) # Capture one or more digits in a group (group 3) )? # Close non capturing group and make it optional ^#在字符串开头断言位置。 (#在组中捕获(组1) \(?#匹配可选的左括号 \d+#匹配一个或多个数字 [/;]#匹配正斜杠或分号 \d+#匹配一个或多个数字 \)?#匹配可选的右括号 )#关闭捕获组 (#在组中捕获(第2组) [A-Z-]+#将大写字符或连字符匹配一次或多次 )#关闭捕获组 (?:#非捕获组 _#匹配下划线 (\d+)#捕获组中的一个或多个数字(组3) )?#关闭非捕获组并将其设置为可选

    在这个例子中,它需要拆分什么?(194;178)RS(0x100)->(194;178)RS(0x100)?@MichaelRandall,对不起。它还应该拆分为a)(126;112)和b)RSSo我想你说的是,第一个组件总是一个分数类型的符号int/int,或者两个被括号包围的值yes。其在括号内或括号内; ^ # Assert position at the beginning of the string. ( # Capture in a group (group 1) \(? # Match optional opening parenthesis \d+ # match one or more digits [/;] # Match forward slash or semicolon \d+ # Match One or more digits \)? # Match optional closing parenthesis ) # Close captured group ( # Capture in a group (group 2) [A-Z-]+ # Match an uppercase character or a hyphen one or more times ) # Close captured group (?: # A non capturing group _ # Match an underscore (\d+) # Capture one or more digits in a group (group 3) )? # Close non capturing group and make it optional