How to parse alphanumeric value?


Author
Message
Edhy Rijo
E
StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
I have a field which may have alphanumeric values like this:



AMDNY-0001253

AMDNJ-0011253

000001253

XXHAM 1253





I need to get the numeric part to do some calculations and then assembly the alpha + numeric part again. Is there any cool .Net function that would help me on this process instead of me looping the value backwards to get only the numeric part of 1253 in the above example?

Edhy Rijo

Replies
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (4.8K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Regex! It is a bit of a pain to learn, but it rocks at solving problems like this. You'd use a regex patterns to match parts of an input string.

This would match the first two examples.



([A-Z]*)-([0-9]*)



This would match the third example:



(0*)([0-9]*)



This would match the last example:



([A-Z]*) ([0-9]*)



This would match either of the first two or the last one:



([A-Z]*)(-| )([0-9]*)





Further, the parentheses are used to capture the matched text. Thus, in each of these examples you could then access the prefix separate from the suffix. I.e. with first pattern, and the input AMDNY-0001253 you could pull out AMDNY and 0001253 separately.



I highly recommend RegExBuddy as a tool to help figure the patterns out.



You then use the Regex object in .NET to work with this stuff:

Dim re As New Regex("(?[A-Z]*)-(?[0-9]*)")

Dim mc As MatchCollection = re.Matches("AMDNY-0001253")

Dim m As Match = mc(0)

Dim prodGroup as Group = m.Groups("prodgroup")

Dim delimGroup as Group = m.Groups("delimiter")

Dim idGroup as Group = m.Groups("id")



MessageBox.Show(prodGroup.Value)

MessageBox.Show(idGroup.Value)




Regex is a big subject, but hopefully this will get you started.

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (4.8K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Whoops, code example got messed up because of less than/greater than signs:



Dim re As New Regex("(?<prodgroup>[A-Z]*)-(?<id>[0-9]*)")

Dim mc As MatchCollection = re.Matches("AMDNY-0001253")

Dim m As Match = mc(0)

Dim prodGroup as Group = m.Groups("prodgroup")

Dim idGroup as Group = m.Groups("id")



MessageBox.Show(prodGroup.Value)

MessageBox.Show(idGroup.Value)

Edhy Rijo
E
StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
Thanks Greg,



I will investigate RegEx even though in my case I don't have a constant delimiter like in sample 3 and I need to get just the numeric part to do the calculations and generate new numbers to be added to the alpha part. If I would have a delimiter I could use the String.Split() but I guess I will need to do a function to scan each character and test for IsNumeric() or Char.IsDigit() from right to left until an alpha is found then split the whole value.



It would be really cool if a RegEx expression could get it right. w00t

Edhy Rijo

GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Threaded View
Threaded View
Edhy Rijo - 15 Years Ago
Greg McGuffey - 15 Years Ago
Greg McGuffey - 15 Years Ago
Edhy Rijo - 15 Years Ago
Edhy Rijo - 15 Years Ago
Greg McGuffey - 15 Years Ago
Edhy Rijo - 15 Years Ago
Charles R Hankey - 15 Years Ago
Greg McGuffey - 15 Years Ago
Ivan George Borges - 15 Years Ago
                     Thanks Charles,

Believe me, taking a quick look at the...
Edhy Rijo - 15 Years Ago
                         Whatever tool you use notice there is a regex tutorial link on the...
Charles R Hankey - 15 Years Ago
                             I think I'd try RegexMagic. It looks really accessible, with the...
Greg McGuffey - 15 Years Ago
                                 Hi Greg and all,

Just to let you know that today I finally...
Edhy Rijo - 15 Years Ago
                                     Glad to hear it! Sounds like RegexMagic is quite helpful.
Greg McGuffey - 15 Years Ago

Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search