How to parse alphanumeric value?


Author
Message
Edhy Rijo
E
StrataFrame VIP (3.7K reputation)StrataFrame VIP (3.7K reputation)StrataFrame VIP (3.7K reputation)StrataFrame VIP (3.7K reputation)StrataFrame VIP (3.7K reputation)StrataFrame VIP (3.7K reputation)StrataFrame VIP (3.7K reputation)StrataFrame VIP (3.7K reputation)StrataFrame VIP (3.7K 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

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (2.7K 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 (2.7K 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 (3.7K reputation)StrataFrame VIP (3.7K reputation)StrataFrame VIP (3.7K reputation)StrataFrame VIP (3.7K reputation)StrataFrame VIP (3.7K reputation)StrataFrame VIP (3.7K reputation)StrataFrame VIP (3.7K reputation)StrataFrame VIP (3.7K reputation)StrataFrame VIP (3.7K 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

Edhy Rijo
E
StrataFrame VIP (3.7K reputation)StrataFrame VIP (3.7K reputation)StrataFrame VIP (3.7K reputation)StrataFrame VIP (3.7K reputation)StrataFrame VIP (3.7K reputation)StrataFrame VIP (3.7K reputation)StrataFrame VIP (3.7K reputation)StrataFrame VIP (3.7K reputation)StrataFrame VIP (3.7K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
Greg, once again, thanks for the RegEx samples but I decided to create a small function to parse the values. Here is the code I came up with:



Private OriginalValues() As String = {"AMDNY-0001253", "AMDNJ-0001253", "0001253", "XXHAM 1253", "1253"}

Private Sub ParseValues()

Dim sb As New System.Text.StringBuilder



'-- Loop each original value and separate the Alpha from the Numeric

For Each stringItem As String In OriginalValues

Dim CharacterEnumerator As CharEnumerator = stringItem.GetEnumerator



Dim AlphaValueCounter As Integer = 0

While (CharacterEnumerator.MoveNext())



If Char.IsDigit(CharacterEnumerator.Current) Then

'-- If the current character is a digit then ignore the zero value

If CharacterEnumerator.Current <> "0" Then

' If the current character is not a zero then we have the current position

' to split or in this case SubString() the original value

Exit While

End If

End If

AlphaValueCounter += 1

End While



'-- Create a string builder output to show the different values

sb.AppendLine("Original Value = " & stringItem)

sb.AppendLine("Alpha value = " & stringItem.Substring(0, AlphaValueCounter))

sb.AppendLine("Numeric value = " & stringItem.Substring(AlphaValueCounter))

sb.AppendLine("---------------------------------------------")

sb.AppendLine()

Next

MessageBox.Show(sb.ToString)

End Sub





Then if you have this code in a form, you will call it like Me.ParseValues() it will simply show a message box with all the original values and the parsed ones.



If anybody come up with a simpler approach, please let us know, it will be greatly appreciated. Hehe

Edhy Rijo

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (2.7K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
I'm glad you got it going.



I'd suggest that when you have some time, you might learn some more about regex. I just barely touched on the power of them. They can get very hairy to work with, but they have tons of power. They are also fast. And this is pretty much exactly what they were designed to do. The SF SyntaxEditor uses regex to do the color highlighting. While the initial learning can be a bear, once you have an understanding of them, they turn many hard problems into rather simple ones. BigGrin
Edhy Rijo
E
StrataFrame VIP (3.7K reputation)StrataFrame VIP (3.7K reputation)StrataFrame VIP (3.7K reputation)StrataFrame VIP (3.7K reputation)StrataFrame VIP (3.7K reputation)StrataFrame VIP (3.7K reputation)StrataFrame VIP (3.7K reputation)StrataFrame VIP (3.7K reputation)StrataFrame VIP (3.7K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
Greg McGuffey (06/15/2010)
I'd suggest that when you have some time, you might learn some more about regex... BigGrin




I agree, I have been wanted to get into RegEx since I was working with VFP and I have read some documentation about them but like you said it make take some deep testing to get the matching expressions correct.



This particular fix looks pretty simple, but when calling this method/function hundreds of time in a process it could take more time than using a properly RegEx expression, so I will explore it too this week.

Edhy Rijo

Charles R Hankey
Charles R Hankey
Advanced StrataFrame User (764 reputation)Advanced StrataFrame User (764 reputation)Advanced StrataFrame User (764 reputation)Advanced StrataFrame User (764 reputation)Advanced StrataFrame User (764 reputation)Advanced StrataFrame User (764 reputation)Advanced StrataFrame User (764 reputation)Advanced StrataFrame User (764 reputation)Advanced StrataFrame User (764 reputation)
Group: Forum Members
Posts: 524, Visits: 30K
Hey Edhy



As you explore regex be sure to check out this free tool - Expresso



http://www.ultrapico.com/Expresso.htm

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (2.7K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Using some kind of tool definitely makes using Regex easier. The tool I use is Regex Buddy:



http://www.regexbuddy.com/



The same company also has a new tool Regex Magic that looks promising:



http://www.regexmagic.com/



It's nice to have choices. BigGrin



Ivan George Borges
Ivan George Borges
Strategic Support Team Member (2.8K reputation)Strategic Support Team Member (2.8K reputation)Strategic Support Team Member (2.8K reputation)Strategic Support Team Member (2.8K reputation)Strategic Support Team Member (2.8K reputation)Strategic Support Team Member (2.8K reputation)Strategic Support Team Member (2.8K reputation)Strategic Support Team Member (2.8K reputation)Strategic Support Team Member (2.8K reputation)
Group: StrataFrame MVPs
Posts: 1.9K, Visits: 21K
Hey, good to know. I've got RegexBuddy, will have a look at RegexMagic too.
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search