从字符串中提取多个值

时间:2016-05-24 19:01:05

标签: c#

我有以下字符串变体:

OFFSET P-1D INTERSECT ~(T13)
OFFSET P-11H INTERSECT ~(T3)
OFFSET P8Y INTERSECT ~(M3)

我需要以这种格式解析字符串中的值:

OFFSET P{val1}{val2} INTERSECT ~({val3}{val4})

例如:

OFFSET P{-11}{H} INTERSECT ~({T}{3})

这样做的最佳方式是什么?

1 个答案:

答案 0 :(得分:0)

正则表达式!

var rg = new System.Text.RegularExpressions.Regex(@"^OFFSET P(-?\d+)([DHY]) INTERSECT ~\(([TM])(\d+)\)$");

var match = rg.Match("OFFSET P-1D INTERSECT ~(T13)");

// match.Groups(1).Value = "-1"
// match.Groups(2).Value = "D"
// match.Groups(3).Value = "T"
// match.Groups(4).Value = "13"

行动中:https://regex101.com/r/bA3dB6/1