简体   繁体   English

正则表达式,捕获组

[英]Regex, capturing groups

Hi I need some help on a regex : 嗨,我需要一些关于正则表达式的帮助:

What I need: 我需要的:

  • only use lines which start with d. 仅使用以d开头的行。
  • first decimal after d should be in a first group. d之后的第一个小数应在第一组中。
  • second decimal after a space should be in the second group . 空格后的第二个小数应该在第二组中
  • all following decimals with a space in front should be added to a third group 后面的所有小数点前都有空格应添加到第三组

This is some sample content: 这是一些示例内容:

test 0.000000 1.05024905 0.00000000
test 0.000000 1.05024905 0.00000000
d 0.000000 1.05024905 0.00000000
d 22.042142 1.63673887 10.93467607 79.06532393 100.93467607 169.06532393 190.93467607 259.06532393 280.93467607 349.06532393
d 30.573646 1.98939080 45.00000000 135.00000000 225.00000000 315.00000000
d 35.000000 1.89275124 0.00000000 90.00000000 180.00000000 270.00000000
d 38.591663 2.22368376 23.21666883 66.78333117 113.21666883 156.78333117 203.21666883 246.78333117 293.21666883 336.78333117
d 42.456724 2.16349500 7.32782836 82.67217164 97.32782836 172.67217164 187.32782836 262.67217164 277.32782836 352.67217164
d 90.000000 3.53392178 38.65670480 51.34329520 128.65670480 141.34329520 218.65670480 231.34329520 308.65670480 321.34329520
d 90.000000 3.32645147 22.47120039 67.52879961 112.47120039 157.52879961 202.47120039 247.52879961 292.47120039 337.52879961
d 90.000000 3.19196954 16.27363186 73.72636814 106.27363186 163.72636814 196.27363186 253.72636814 286.27363186 343.72636814
d 90.000000 3.16746506 15.19498176 74.80501824 105.19498176 164.80501824 195.19498176 254.80501824 285.19498176 344.80501824
d 90.000000 3.11573668 12.82369963 77.17630037 102.82369963 167.17630037 192.82369963 257.17630037 282.82369963 347.17630037

I want to use it like following: 我想像下面这样使用它:

string pattern = $@"d +(?<{Grp1}>\-?[0-9]*\.[0-9]*) (?<{Grp2}>\-?[0-9]*\.[0-9]*) (?<{Grp3}>\-?[0-9]*\.[0-9]*)(.*)";

var matches = Regex.Matches(content, regexPattern);
foreach (Match match in matches)
{
    var group1 = match.Groups[Grp1];
    var group2 = match.Groups[Grp2];
    var group3 = match.Groups[Grp3];
    foreach (var capture in group3.Captures)
    {

    }
}

Can't get a correct regex.. 无法获得正确的正则表达式。

Thanks in advance. 提前致谢。

^d (\d+\.\d+) (\d+\.\d+)(.*)$

This is the expression I came up with, it can most likely be improved. 这是我想出的一种表达方式,很可能会得到改善。

It's worth noting that when you use this, you'll wanna use the MultiLine modifier in your expression so that ^ matches the start of every new line. 值得注意的是,当您使用此选项时,您将要在表达式中使用MultiLine修饰符,以使^匹配每个新行的开头。

I would use the following pattern 我将使用以下模式

^d ([\d.]+) ([\d.]+) ?([\d. ]+)?

Test here: https://regex101.com/r/4nQGNc/1 在这里测试: https//regex101.com/r/4nQGNc/1

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

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