Solved

Extract text

Posted on 2013-02-06
8
308 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

729 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