2016-12-29 71 views
3

我試圖找到一種方法來從字符串中提取單詞,只要它包含該單詞中的3個或更多數字/數字。這也將需要返回像正則表達式搜索包含3個或更多數字的字符串

TX-23443FUX3329442等整個文本...

從我發現

\w*\d\w* 

破折號之前不會返回任何字母像第一個例子?

我在網上找到的所有例子似乎都不適合我。任何幫助表示讚賞!

+1

u能顯示您的字符串的外觀。到目前爲止您嘗試過的產品的確切輸出是什麼? –

+0

我忘了提及它也需要返回整個文本,如 – mike11d11

+0

我忘了提及它也需要返回整個文本,如TX-23443或FUX3329442等......從我發現的「\ w * \ d \ w *「不會像第一個例子那樣在短劃線之前返回任何字母? – mike11d11

回答

0

試試這個:

string strToCount = "Asd343DSFg534434"; 
int count = Regex.Matches(strToCount,"[0-9]").Count; 
2

如果我正確理解你的問題,你想找到所有包含3+ consequtive號碼就如TX-23443或FUX3329442所以你想提取TX-23443字符串和FUX3329442即使它包含-之間的字符串。因此,這裏是這可能會幫助你

string InpStr = "TX-23443 or FUX3329442"; 
MatchCollection ms = Regex.Matches(InpStr, @"[A-Za-z-]*\d{3,}"); 
foreach(Match m in ms) 
{ 
    Console.WriteLine(m); 
} 
2

這一個應該做的伎倆假設你的「話」解只標準拉丁單詞字符:A-Z,A-Z,0-9和_。

Regex word_with_3_digits = new Regex(@"(?#!cs word_with_3_digits Rev:20161129_0600) 
    # Match word having at least three digits. 
    \b   # Anchor to word boundary. 
    (?:   # Loop to find three digits. 
     [A-Za-z_]* # Zero or more non-digit word chars. 
     \d   # Match one digit at a time. 
    ){3}   # End loop to find three digits. 
    \w*   # Match remainder of word. 
    \b   # Anchor to word boundary. 
    ", RegexOptions.IgnorePatternWhitespace); 
1

在javascript中我會寫這樣一個正則表達式:

\ S * \ d {3,} \ S *

我製備的online test

0

即使最後還有短跑,這個人似乎也在爲我工作。

[ - ] \ W [ - ] \ d {3} [ - ] \ W * [ - ] \ W

相關問題