正则表达式剥离值

时间:2014-03-07 12:30:22

标签: c# regex vb.net

从字符串“A123.456C-456.789F987321”我需要返回

  • A123.456
  • C-456.789
  • F987321

这可以是3个单独的呼叫,如“A”(或“C”或“F”后跟任何十进制数或小数点或符号(“+”或“ - ”),直到下一个非数字(或小数点或符号)字符,或任何字母后跟任何数字或小数的更通用的调用,直到下一个非数字(或小数点或符号)

由于

编辑:

为了清楚起见,我应该使用正则表达式来表示X返回

123.456 Where X = "A"
-456.789 Where X = "C"
987321 Where X = "F"

来自字符串“A123.456C-456.789F987321”

3 个答案:

答案 0 :(得分:1)

试试这个:

(\w\-?[\d\.]+)

说明:

\w match any word character [a-zA-Z0-9_]
\-? matches the character - literally
       Quantifier: Between zero and one time, as many times as possible, giving back as needed [greedy]
[\d\.]+ match a single character present in the list below
       Quantifier: Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\d match a digit [0-9]
\. matches the character . literally

g modifier: global. All matches (don't return on first match)

演示:

http://regex101.com/r/iG6lD1

答案 1 :(得分:0)

您正在进行重叠正则表达式匹配。你的正则表达式将是:

(?=([A-Z][^A-Z]+))

这里选择一个大写字母,然后选择非大写字母作为一个组。

您可以根据需要轻松修改此示例(包含其他字符的字母数字!)

答案 2 :(得分:0)

void Main(string[] args)
{
        var regVal  = "A123.456C-456.789F987321";

        string pattern =@"([A-Z][-]*[0-9|.]+)";
        foreach (Match match in Regex.Matches(regVal, pattern, RegexOptions.IgnoreCase))
        Console.WriteLine(match.Groups[1].Value);

}

输出是:

A123.456

C-456.789

F987321