我需要找到正則表達式的幫助不大適合我的需要,我想是可以輸入任意的字符串,正則表達式找到任何和所有的整數值,並返回他們,說數字的正則表達式?
string s1 = "This is some string"
string s2 = " this is 2 string"
string s3 = " this is 03 string"
string s4 = "4 this is the 4th string"
string s5 = "some random string: sakdflajdf;la 989230"
string s6 = "3494309 [email protected]# 234234"
現在我要的是正則表達式返回,
for s1 = return null (// nothing)
s2 = return 2
s3 = return 0 and 3 (// may separately as 0 and 3 or together as 03 doesn't matter)
s4 = return 4 and 4 (// it has 4 2 times right?)
s5 = 989230 (// may together as this or separately as 9 8 9 2 3 0 again is unimportant, but what's important is that it should return all integer values)
s6 = 3494309 and 234234 (// again may they be together as this or like this 3 4 9 4 3 0 9 and 2 3 4 2 3 4 that is unimportant, all that is imp is that it should return all integers)
我已經試過[0-9]
,\d
和^.*[0-9]+.*$
,但他們都不似乎工作。任何人都可以幫忙嗎?
PS:請參閱Rename file using regular expression
你是如何應用表達式的?請發佈您的代碼。也請澄清你的意思*,但他們似乎沒有工作*。什麼不工作?你得到的結果是什麼? '\ d'只匹配一個數字,'^。* [0-9] +。* $'匹配整個字符串,如果它至少包含一個數字。看來你想要'\ d +'。 – 2012-04-11 20:02:51
下面的答案會在您的輸入中找到數字。您可以編寫一個方法,返回給定數字的值。我建議使用switch語句。 – jac 2012-04-11 20:10:28
請查看[鏈接]上的更新問題(http://stackoverflow.com/questions/10117237/rename-file-using-regular-expression) – Razort4x 2012-04-12 04:10:41