2014-03-07 135 views
0

从字符串 “A123.456C-456.789F987321” 我需要返回正则表达式值条

  • A123.456
  • C-456.789
  • F987321

这可以是在下一个非数字(或小数点或符号)字符之前,将3个单独的电话作为“A”(或“C”或“F”,后跟任何小数或小数点或符号(“+”或“ - ” ,或任何字母后跟任意数字或小数的更一般的呼叫直到下一个非数字(或decimalpoint或符号)

感谢

编辑:

为清楚起见,我应该在X方面使用什么正则表达式返回

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

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

+0

请有关问题明确。 –

+0

@KarthikAMR编辑清晰度 – robpws

回答

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

+0

感谢您的链接 – robpws

0

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

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

这里是选择大写字母和大于非大写字母作为一个组。

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

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

     string pattern [email protected]"([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