3D位置的正则表达式

时间:2012-05-10 21:14:06

标签: .net regex

我希望在.NET DataGridView中验证空间中的一个点作为输入。 DataGridViewTextBoxCell的输入应采用以下形式:

[1.0, 1.0, 1.0]

上面的1.0表示X,Y,Z轴(也可能是0.1.1)。它还必须包含打开和关闭括号 因为它是一个位置,它可能是负数或正数,并且在小数点后面有n个位置。

1 个答案:

答案 0 :(得分:0)

如果您正在寻找RegEx,它应该是这样的:

^ // The string should start with the left bracer
\[ // left bracer

([-+]?[0-9]*\.?[0-9]+), // first float value followed by a virgule, this is matched
([-+]?[0-9]*\.?[0-9]+), // second float value followed by a virgule, this is matched
([-+]?[0-9]*\.?[0-9]+) // third float value, this is matched

\] // right bracer
$ // The string should end with the right bracer

没有评论,这会给你:

^\[([-+]?[0-9]*\.?[0-9]+),([-+]?[0-9]*\.?[0-9]+),([-+]?[0-9]*\.?[0-9]+)\]$

如果希望正则表达式忽略任何空格,可以在每个元素之间添加\ s *。

^\s*\[\s*([-+]?[0-9]*\.?[0-9]+)\s*,\s*([-+]?[0-9]*\.?[0-9]+)\s*,\s*([-+]?[0-9]*\.?[0-9]+)\s*\]\s*$