Results 1 to 5 of 5

Thread: Regex..can i do this?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    413

    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

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Regex..can i do this?

    How about a little function to format the part nunmber? Something like this:
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, _
    2.                        ByVal e As System.EventArgs) Handles Button1.Click
    3.  
    4.         Dim test() As String = {"1245", "1245-2A", "C1125-12CB", "V11259"}
    5.         Dim display As String = ""
    6.         For Each str As String In test
    7.             display &= FormatPartNumber(str, "x"c, ) & vbCrLf
    8.         Next
    9.         MsgBox(display)
    10.     End Sub
    11.  
    12.     Public Function FormatPartNumber(ByVal partNum As String, _
    13.                                      Optional ByVal paddingChar1 As Char = " "c, _
    14.                                      Optional ByVal paddingChar2 As Char = "0"c) _
    15.                                      As String
    16.         Dim retString As String = ""
    17.         Dim prefix As String = ""
    18.         Dim number As String = ""
    19.         Dim suffix As String = ""
    20.         Dim strTemp As String() = partNum.Split("-"c)
    21.         If strTemp.Length > 1 Then
    22.             suffix = "-" & strTemp(1)
    23.         End If
    24.         Dim chrArray As Char() = strTemp(0).ToCharArray
    25.         For Each ch As Char In chrArray
    26.             If Char.IsLetter(ch) Then
    27.                 prefix &= ch.ToString()
    28.             Else
    29.                 number &= ch.ToString()
    30.             End If
    31.         Next
    32.         retString = prefix.PadLeft(3, paddingChar1) & _
    33.                     number.PadLeft(6, paddingChar2) & _
    34.                     suffix
    35.         Return retString
    36.     End Function

  3. #3
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    413

    Re: Regex..can i do this?

    Quote 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

  5. #5
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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
  •  



Click Here to Expand Forum to Full Width