C# 如何从文本中拆分带有符号的数字

C# 如何从文本中拆分带有符号的数字,c#,regex,C#,Regex,我正在尝试从文本中拆分带有符号的数字。我正在使用正则表达式。例如,我有一个等式: 12X-3Y我认为您非常接近,只需使用\d而不是\d,捕获-,并使用Regex.Match而不是split: (-?\d+) 用法 您可以将这些捕获串在一起,如下所示: var s = string.Join(" ", match.Captures .Select(c => c.Value) .ToArray()); string text = @"12x-3y<

我正在尝试从文本中拆分带有符号的数字。我正在使用正则表达式。例如,我有一个等式:


12X-3Y我认为您非常接近,只需使用
\d
而不是
\d
,捕获
-
,并使用Regex.Match而不是split:

(-?\d+)

用法 您可以将这些
捕获
串在一起,如下所示:

var s = string.Join(" ", match.Captures
    .Select(c => c.Value)
    .ToArray());
string text = @"12x-3y<=-6" ;
Regex rx = new Regex( @"-?\d+(\.\d+)?([Ee][+-]?\d+)?") ;
string[] words = rx
                 .Matches(text)
                 .Cast<Match>()
                 .Select( m => m.Value )
                 .ToArray()
                 ;

List strList=new List();
List fltList=新列表();
StringBuilder sb=新的StringBuilder();
对于(int i=0;i0)
strList.Add(sb.ToString());
(某人清楚地);
}
其他的
sb.Add(richTextBox.Text[i]);
}
浮努穆特;
foreach(strList中的字符串数)
{
if(float.TryParse(num,out numOut))
fltList.Add(numOut);
}

不是最漂亮的,但它应该有用。

你可以试试这样的东西:

var s = string.Join(" ", match.Captures
    .Select(c => c.Value)
    .ToArray());
string text = @"12x-3y<=-6" ;
Regex rx = new Regex( @"-?\d+(\.\d+)?([Ee][+-]?\d+)?") ;
string[] words = rx
                 .Matches(text)
                 .Cast<Match>()
                 .Select( m => m.Value )
                 .ToArray()
                 ;
轻松点

正则表达式应该匹配任何base-10文本。它可以分为以下几个部分:

-?              # an optional minus sign, followed by
\d+             # 1 or more decimal digits, followed by
(               # an optional fractional component, consisting of
  \.            # - a decimal point, followed by
  \d+           # - 1 or more decimal digits.
)?              # followed by
(               # an optional exponent, consisting of
  [Ee]          # - the letter "E" , followed by
  [+-]?         # - an optional positive or negative sign, followed by
  \d+           # - 1 or more decimal digits
)?              #

你想要实现什么?
words[0] = "12
words[2] = "-3"
words[4] = "-6"
-?              # an optional minus sign, followed by
\d+             # 1 or more decimal digits, followed by
(               # an optional fractional component, consisting of
  \.            # - a decimal point, followed by
  \d+           # - 1 or more decimal digits.
)?              # followed by
(               # an optional exponent, consisting of
  [Ee]          # - the letter "E" , followed by
  [+-]?         # - an optional positive or negative sign, followed by
  \d+           # - 1 or more decimal digits
)?              #