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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex format problem

Status
Not open for further replies.

DotNetBlocks

Programmer
Apr 29, 2004
161
0
0
US
Hello, I have a string that i want to fit a specific format, but when i try and apply it i get an error.
What am i doing wrong?

Code:
dim chekme as string  ="999999999999"
  chekme = Format(chekme, "^\d{5}-\d{2}-\d{3}-\d{2}$")
  ' It should look like this "99999-99-999-99"

I get the following error:
Code:
Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
 
your parameters are switched first. Error indicates you are not matching your arguments though. Mainly {0} which is the first.

Take a look at .NET Format String 101

I wouldn't give you a link and attempt to explain the Format method more but to be honest I have not dove into it that much so I may mislead you. It does seem like you want to do a RegEx.Replace though and not a String.Format.

Although in your case and after you read that article and possibly MS references you would I believe come up with something like this. Looks horribly inefficient though to me

Code:
Dim chekme As String = "123456789123"
Dim newStr As String = String.Format("{0}-{1}-{2}-{3}", _
chekme.Substring(0, 4), _
chekme.Substring(5, 2), _
chekme.Substring(7, 3), _
chekme.Substring(10, 2))


____________ signature below ______________
General FAQ faq333-2924
5 steps to asking a question faq333-3811
 
Imports System.Text.RegularExpressions

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim chekme As String = "999999999999"
Dim re As New Regex("(\d{5})(\d{2})(\d{3})(\d{2})")

chekme = re.Replace(chekme, "$1-$2-$3-$4")
Debug.Print(chekme)
End Sub
End Class
 
I was hoping there would be some kind of learning experience in saying that would be the way to go ;-)


____________ signature below ______________
General FAQ faq333-2924
5 steps to asking a question faq333-3811
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top