2016-04-26 105 views
0

正則表達式對我來說很新的,所以我有一個問題。 我在C#中搜索正則表達式模式來查找1.60GHz的在一個字符串。這是一個例子。C#中的正則表達式查找CPU時鐘頻率

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

我想有1.60GHz的

非常感謝。

+0

會不會有永遠是一個小數,它總是會GHz的? –

+0

是的,永遠是一個小數和千兆赫 – JohnDoe

回答

0

好吧,我找到了解決辦法

(\ d + (? :?\ d +)[千兆克] [HH] [ZZ])

感謝大家!

+0

不知道這是故意的,但整數是不行的(3GHz的不匹配) –

+0

你說得對。但在我的情況下沒有小數沒有價值。所以模式是X.XXGHz – JohnDoe

1

你可以使用這個表達式

(\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 
2

事情是這樣的:

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

他們從來不說他們想單獨的頻率,爲什麼在複雜的人說,他們誰是初學者? –

+0

@Alfie Goodacre:只是爲了演示如何做到這一點(如果需要),並阻止獲取子字符串。 –