mike11d11
mike11d11

Reputation: 41

Regex searching for string that contains 3 or more digits

I'm trying to find a way to extract a word from a string only if it contains 3 or more digits/numbers in that word. It would also need to return the entire text like

TX-23443 or FUX3329442 etc...

From what I found

\w*\d\w*

won't return the any letters before the dash like the first example?

All the example I found online don't seem to be working for me. Any help is appreciated!

Upvotes: 4

Views: 10676

Answers (5)

mike11d11
mike11d11

Reputation: 41

This one seems to be working for me even if there is a dash at the end as well.

[-]\w[-]\d{3,}[-]\w*[-]\w

Upvotes: 0

Fabrizio
Fabrizio

Reputation: 8043

In javascript I would write a regex like this:

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

I've prepared an online test.

Upvotes: 1

ridgerunner
ridgerunner

Reputation: 34395

This one should do the trick assuming your "words" have only the standard latin word characters: A-Z, a-z, 0-9 and _.

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);

Upvotes: 1

Mohit S
Mohit S

Reputation: 14044

IF I understand your question correctly you wanted to find all the string which contains 3+ consequtive numbers in it such as TX-23443 or FUX3329442 so you wanted to extract TX-23443 and FUX3329442 even if it contains - in between the string. So here is the solution which might help you

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

Upvotes: 3

Kalyan
Kalyan

Reputation: 1200

Try this:

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

Upvotes: 0

Related Questions