Link to home
Start Free TrialLog in
Avatar of zattz
zattz

asked on

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
Avatar of kaufmed
kaufmed
Flag of United States of America image

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

Avatar of zattz
zattz

ASKER

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.
The "\d" in the pattern should account for numbers. Can you provide some sample inputs?
Avatar of zattz

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Actually, I need to revise the above to account for the spaces. Uno momento.
Updated:

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

Open in new window

Avatar of zattz

ASKER

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