Solved

Extract text

Posted on 2013-02-06
8
305 Views
Last Modified: 2013-02-06
Hi

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

Thanks
0
Comment
Question by:zattz
  • 5
  • 3
8 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38859343
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
 

Author Comment

by:zattz
ID: 38859749
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38859884
The "\d" in the pattern should account for numbers. Can you provide some sample inputs?
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:zattz
ID: 38859990
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
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 38860027
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38860044
Actually, I need to revise the above to account for the spaces. Uno momento.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38860087
Updated:

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

Open in new window

0
 

Author Comment

by:zattz
ID: 38860138
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

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question