c#Regex在字符串中查找CPU时钟速度

c#Regex在字符串中查找CPU时钟速度,c#,.net,regex,C#,.net,Regex,正则表达式对我来说是非常新的,所以我有一个关于它的问题。 我正在C#中搜索正则表达式模式,以在字符串中找到1.60GHz。这里有一个例子 英特尔(R)Atom(TM)CPU Z3795@1.60GHz 我想要1.60GHz 非常感谢。您可以使用这个正则表达式 (\d+(?:\.?)\d+GHz) 说明: ( - //Start group \d - //Any digit + - //Capture as many digits as possible before a non digit c

正则表达式对我来说是非常新的,所以我有一个关于它的问题。 我正在C#中搜索正则表达式模式,以在字符串中找到1.60GHz。这里有一个例子

英特尔(R)Atom(TM)CPU Z3795@1.60GHz

我想要1.60GHz


非常感谢。

您可以使用这个正则表达式

(\d+(?:\.?)\d+GHz)
说明:

( - //Start group
\d - //Any digit
+ - //Capture as many digits as possible before a non digit character
(?: - //Start a new group which we do not want to match seperately
\.? - //Match a decimal point literally or none at all
) - //End the non capturing group
\d+ - //Same as above
GHz - //Match the literal string "GHz"
) - //Finally close the group

你可以用这个正则表达式

(\d+(?:\.?)\d+GHz)
说明:

( - //Start group
\d - //Any digit
+ - //Capture as many digits as possible before a non digit character
(?: - //Start a new group which we do not want to match seperately
\.? - //Match a decimal point literally or none at all
) - //End the non capturing group
\d+ - //Same as above
GHz - //Match the literal string "GHz"
) - //Finally close the group
大概是这样的:

  String pattern = @"(?<Freq>[0-9]+(\.[0-9]+)?)GHz";

  String source = "Intel(R) Atom(TM) CPU Z3795 @ 1.60GHz";

  // 1.60GHz
  String result = Regex.Match(source, pattern).Value;
  // 1.60
  String frequency = Regex.Match(source, pattern).Groups["Freq"].Value;
字符串模式=@“(?[0-9]+(\[0-9]+)?)GHz”;
String source=“英特尔(R)原子(TM)CPU Z3795@1.60GHz”;
//1.60GHz
字符串结果=Regex.Match(源、模式).Value;
// 1.60
字符串频率=Regex.Match(源、模式)。组[“Freq”]。值;
类似这样的内容:

  String pattern = @"(?<Freq>[0-9]+(\.[0-9]+)?)GHz";

  String source = "Intel(R) Atom(TM) CPU Z3795 @ 1.60GHz";

  // 1.60GHz
  String result = Regex.Match(source, pattern).Value;
  // 1.60
  String frequency = Regex.Match(source, pattern).Groups["Freq"].Value;
字符串模式=@“(?[0-9]+(\[0-9]+)?)GHz”;
String source=“英特尔(R)原子(TM)CPU Z3795@1.60GHz”;
//1.60GHz
字符串结果=Regex.Match(源、模式).Value;
// 1.60
字符串频率=Regex.Match(源、模式)。组[“Freq”]。值;

好的,我找到了一个解决方案

(\d+(?:。.d+)[Gg]?[Hh]?[Zz])


谢谢大家

好的,我找到了解决办法

(\d+(?:。.d+)[Gg]?[Hh]?[Zz])


谢谢大家

总是有一个小数点,它总是GHz吗?是的,总是有一个小数点,GHz总是有一个小数点,它总是GHz吗?是的,总是有一个小数点,GHz他们从来没有说过他们想要单独的频率,为什么对于说自己是初学者的人来说过于复杂?@Alfie Goodacre:只是为了演示如何做(如果需要)并阻止获得子字符串。他们从来没有说过他们想要单独的频率,为什么对于说自己是初学者的人来说过于复杂?@Alfie Goodacre:只是为了演示如何做(如果需要)不鼓励获取子字符串。不确定这是否是有意的,但整数将不起作用(3GHz将不匹配)您是对的。但在我的例子中,没有小数就没有值。所以模式是X.xxGHz不确定这是否是故意的,但整数将不起作用(3GHz将不匹配)您是对的。但在我的例子中,没有小数就没有值。所以这个模式是X.XXGHz