Solved

Extract text

Posted on 2013-02-06
8
307 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

733 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