|
-
Oct 27th, 2006, 01:38 PM
#1
Thread Starter
Hyperactive Member
Regex..can i do this?
I have several part numbers that I need to convert to a fixed format.
AAA000000AAAAA
If the first Charicter(s) is an Alpha, then I need to output it in the first three spots and pad it with spaces so it takes up three spaces. Then, the following will aways be a number and need to make it 6 chars and pad with zero's in the front. The last part always comes with a "-" so it just need to be appended to the end.
I have part numbers like this and show what they need to be turned into.
<sp> = space char
In P/N -----------------Out P/N
1245 ------------------<sp><sp><sp>001245
1245-2A----------------<sp><sp><sp>001245-2A
C1125-12CB ------------<sp><sp>C001125-12CB
V11259------------------<sp><sp>V011259
I hope this is clear? Can it be done in a simple RegEx?
Visual Studio .NET 2005/.NET Framework 2.0
-
Oct 27th, 2006, 02:47 PM
#2
Re: Regex..can i do this?
How about a little function to format the part nunmber? Something like this:
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim test() As String = {"1245", "1245-2A", "C1125-12CB", "V11259"}
Dim display As String = ""
For Each str As String In test
display &= FormatPartNumber(str, "x"c, ) & vbCrLf
Next
MsgBox(display)
End Sub
Public Function FormatPartNumber(ByVal partNum As String, _
Optional ByVal paddingChar1 As Char = " "c, _
Optional ByVal paddingChar2 As Char = "0"c) _
As String
Dim retString As String = ""
Dim prefix As String = ""
Dim number As String = ""
Dim suffix As String = ""
Dim strTemp As String() = partNum.Split("-"c)
If strTemp.Length > 1 Then
suffix = "-" & strTemp(1)
End If
Dim chrArray As Char() = strTemp(0).ToCharArray
For Each ch As Char In chrArray
If Char.IsLetter(ch) Then
prefix &= ch.ToString()
Else
number &= ch.ToString()
End If
Next
retString = prefix.PadLeft(3, paddingChar1) & _
number.PadLeft(6, paddingChar2) & _
suffix
Return retString
End Function
-
Oct 27th, 2006, 04:24 PM
#3
Re: Regex..can i do this?
Probably be easier to do it with string manipulation than with regex, like the above post had mentioned.
Last edited by gigemboy; Oct 27th, 2006 at 04:28 PM.
-
Oct 28th, 2006, 01:39 PM
#4
Thread Starter
Hyperactive Member
Re: Regex..can i do this?
 Originally Posted by gigemboy
Probably be easier to do it with string manipulation than with regex, like the above post had mentioned.
StanAV code works great. I'd like see it done with Regex to.? I did a goolge search on RegEx and downloaded some editpro pad..or something like that to do the tutorials. I had to restore my computer because I think it installed some virus or something. My machine got real pokie after I installed it. Hopefully the restore will work. I want home after I started the restore so I wont know till monday. Anyway, if anyone is good with regex expressions I would like to see it done that way.
Thanks, Dave
Visual Studio .NET 2005/.NET Framework 2.0
-
Oct 28th, 2006, 03:08 PM
#5
Re: Regex..can i do this?
Well regular expressions are really made to "find" text that matches a pattern. I could see it being used to find the actual part numbers in some sort of text, but you already have the part numbers. It would probably require several expressions, I believe, one in order to match on the text inside of the part number you want, and then a regex replace (or regular string replace) statement to replace it with the text you want.
I have used Regex several times, and when you want to start replacing text that you are matching on, it gets a little tricky. Its not as easy as it sounds and I don't really know how to do it without writing out several different statements.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|