Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse a string to find any strings of length 'x'

Status
Not open for further replies.

steve4king

IS-IT--Management
Feb 6, 2007
154
US
This seems like it should be pretty simple using indexof and a looping until end of string.

My input will be a string value pasted into a text box that will look something like this:

Code:
Here's some of the items I was talking about Jamie.
10012345
10012346, 10013417
10012348,10012349

Can you fix those for me?
Thanks,
Jim

I need to be able to pull out all of those eight digit numbers. However.. I'm not sure how to identify those, they may not have a leading or trailing space, they may have a space, tab or comma delimiter.

Any ideas?
Thanks!
-Stephen
 
Look up Regular Expressions.

You can have it find any matching patterns (in this case the 8 digit numbers) and you wouldn't care about the delimiter. A pattern like \d{8} would retrieve 8 digit numbers...
 
Ahh yes, that's exactly what I needed. Thanks!
That part is now working.

One other little issue though.. I wanted to be able to retrieve the shorthand versions as well.
if the code is 10000123 it can be written 1-123.
The problem is I can't know how many characters the second half will be.

Code:
pattern = @"\b\d{1}-\d{?}\b";
            Regex rgx = new Regex(pattern);
            foreach (Match match in rgx.Matches(sInput))
            {
                doStuff(match.Value);
            }
As I'm sure you know the question mark in my code above will not do what I need.. Neither will [1-9] as I've found.

I have the code working, but only by parsing the same text eight different times.. Not as pretty as I'd like it.
 
If you want to limit it to 7 digits past the - but need a leading digit...

you could use this pattern @"\b\d{1}-\d{1,7}\b"

A decent free regex editor/tester is Express @
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top