Extract text

Hi

How can I best extract in c# using a regex an 8 digit mix of numbers and letters? possibly with spaces between

Thanks
zattzAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
käµfm³d 👽Connect With a Mentor Commented:
OK, I thought by "mix" you meant either all letters, all numbers, or all letters and numbers. I see now that you mean the latter.

Try:

\b(?=[a-zA-Z]*\d)(?=\d*[a-zA-Z])[a-zA-Z\d]{8}\b

Open in new window

0
 
käµfm³d 👽Commented:
Slight modification to the comment I posted in your other question:

using System.Text.RegularExpressions;

...

Match m = Regex.Match(targetString, @"\b(?:[ ]*[a-zA-Z\d]){8}\b");

if (m.Success)
{
    string numericalValue = m.Groups[0].Value;
}

Open in new window

0
 
zattzAuthor Commented:
Hi

Having a bit of a problem with this, the string will always contain a mix of both numbers and letters. At the moment the regex is picking up strings with just letters.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
käµfm³d 👽Commented:
The "\d" in the pattern should account for numbers. Can you provide some sample inputs?
0
 
zattzAuthor Commented:
The matches should be a mix including BOTH numbers and letters.

I have a large body of text, and "whatsapp" is being returned. This does not include both numbers and letters, and so should not match.
0
 
käµfm³d 👽Commented:
Actually, I need to revise the above to account for the spaces. Uno momento.
0
 
käµfm³d 👽Commented:
Updated:

\b(?=[ \d]*[a-zA-Z])(?=[ a-zA-Z]*\d)(?: *[a-zA-Z\d]){8}\b

Open in new window

0
 
zattzAuthor Commented:
The last solution that deals with spaces picked up "february", but never mind, the example above is working fine and I don't think I will see any spaces. Thank you so much for the help! I appreciate it
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.