Need help parsing a string - reverse Val?

  • Thread starter Thread starter google
  • Start date Start date
The function I coded handles your sequence just fine. input "AH-1-G1" output "AH-1-G2". n'est pas?

The one thing it doesn't seem to handle is leading zeros, e.g. input
"AB001", output "AB2" (unless you count the trivial case where the
string contains only digits).
 
John Nurick said:
output "AH-1-G2". n'est pas?

The one thing it doesn't seem to handle is leading zeros, e.g. input
"AB001", output "AB2" (unless you count the trivial case where the
string contains only digits).
 
Rather than let a good thread die how about :-

Function IncNum(str As String) As String
'--------------------------------
IncNum = ""

If Len(str) < 1 Then
Exit Function
End If
'--------------------------------
Dim Result As String

Dim re As New RegExp
Dim ms As MatchCollection
Dim m As Match

Dim Digits As String
Dim Incr As Long
Dim i As Long
'--------------------------------
Result = str

re.Pattern = "([0-9]+)"
re.Global = True
re.IgnoreCase = True
Set ms = re.Execute(Result)

Incr = 1
'--------------------------------
For i = ms.Count - 1 To 0 Step -1
Set m = ms(i)

Digits = m.Value
strFormat = String(m.Length, "0")

Digits = Format(Digits + Incr, strFormat)
If Len(Digits) > m.Length Then
Digits = Mid(Digits, 2)
Else
Incr = 0
End If

Mid(Result, m.FirstIndex + 1, m.Length) = Digits

Next 'm
'--------------------------------
IncNum = Result

If Incr > 0 Then
Err.Raise 5, Err.Source, "Unable to increment code string."
End If
'--------------------------------
End Function

Requires reference to ms regular expressions 1.0 or 5.5

Try
?IncNum("000asd999.999.999")

Anyone keeping count of the ways?

Regards John
 
As I pointed out elsewhere in this thread, you don't have to have a
reference to MS Regular Expressions to use regular expressions: you can use
Late Binding. (Note that you're missing a declaration for strFormat)

Function IncNum(str As String) As String
'--------------------------------
IncNum = ""

If Len(str) < 1 Then
Exit Function
End If
'--------------------------------
Dim Result As String

Dim re As Object
Dim ms As Object
Dim m As Object

Dim Digits As String
Dim Incr As Long
Dim i As Long
Dim strFormat As String
'--------------------------------
Result = str

Set re = CreateObject("VBScript.RegExp")
re.Pattern = "([0-9]+)"
re.Global = True
re.IgnoreCase = True
Set ms = re.Execute(Result)

Incr = 1
'--------------------------------
For i = ms.Count - 1 To 0 Step -1
Set m = ms(i)

Digits = m.Value
strFormat = String(m.Length, "0")

Digits = Format(Digits + Incr, strFormat)
If Len(Digits) > m.Length Then
Digits = Mid(Digits, 2)
Else
Incr = 0
End If

Mid(Result, m.FirstIndex + 1, m.Length) = Digits

Next 'm
'--------------------------------
IncNum = Result

If Incr > 0 Then
Err.Raise 5, Err.Source, "Unable to increment code string."
End If
'--------------------------------
End Function


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)



John Griffiths said:
Rather than let a good thread die how about :-

Function IncNum(str As String) As String
'--------------------------------
IncNum = ""

If Len(str) < 1 Then
Exit Function
End If
'--------------------------------
Dim Result As String

Dim re As New RegExp
Dim ms As MatchCollection
Dim m As Match

Dim Digits As String
Dim Incr As Long
Dim i As Long
'--------------------------------
Result = str

re.Pattern = "([0-9]+)"
re.Global = True
re.IgnoreCase = True
Set ms = re.Execute(Result)

Incr = 1
'--------------------------------
For i = ms.Count - 1 To 0 Step -1
Set m = ms(i)

Digits = m.Value
strFormat = String(m.Length, "0")

Digits = Format(Digits + Incr, strFormat)
If Len(Digits) > m.Length Then
Digits = Mid(Digits, 2)
Else
Incr = 0
End If

Mid(Result, m.FirstIndex + 1, m.Length) = Digits

Next 'm
'--------------------------------
IncNum = Result

If Incr > 0 Then
Err.Raise 5, Err.Source, "Unable to increment code string."
End If
'--------------------------------
End Function

Requires reference to ms regular expressions 1.0 or 5.5

Try
?IncNum("000asd999.999.999")

Anyone keeping count of the ways?

Regards John
 
Douglas J. Steele said:
As I pointed out elsewhere in this thread, you don't have to have a
reference to MS Regular Expressions to use regular expressions: you can use
Late Binding. (Note that you're missing a declaration for strFormat)

Thanks, I refactored out some stuff and removed that at the same time :(
John


Function IncNum(str As String) As String
'--------------------------------
IncNum = ""

If Len(str) < 1 Then
Exit Function
End If
'--------------------------------
Dim Result As String

Dim re As Object
Dim ms As Object
Dim m As Object

Dim Digits As String
Dim Incr As Long
Dim i As Long
Dim strFormat As String
'--------------------------------
Result = str

Set re = CreateObject("VBScript.RegExp")
re.Pattern = "([0-9]+)"
re.Global = True
re.IgnoreCase = True
Set ms = re.Execute(Result)

Incr = 1
'--------------------------------
For i = ms.Count - 1 To 0 Step -1
Set m = ms(i)

Digits = m.Value
strFormat = String(m.Length, "0")

Digits = Format(Digits + Incr, strFormat)
If Len(Digits) > m.Length Then
Digits = Mid(Digits, 2)
Else


'Alternative completion
Mid(Result, m.FirstIndex + 1, m.Length) = Digits
IncNum = Result

Exit Function
End If

Mid(Result, m.FirstIndex + 1, m.Length) = Digits

Next 'm
'--------------------------------
IncNum = Result

If Incr > 0 Then
Err.Raise 5, Err.Source, "Unable to increment code string."
End If
'--------------------------------
End Function


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)



John Griffiths said:
Rather than let a good thread die how about :-

Function IncNum(str As String) As String
'--------------------------------
IncNum = ""

If Len(str) < 1 Then
Exit Function
End If
'--------------------------------
Dim Result As String

Dim re As New RegExp
Dim ms As MatchCollection
Dim m As Match

Dim Digits As String
Dim Incr As Long
Dim i As Long
'--------------------------------
Result = str

re.Pattern = "([0-9]+)"
re.Global = True
re.IgnoreCase = True
Set ms = re.Execute(Result)

Incr = 1
'--------------------------------
For i = ms.Count - 1 To 0 Step -1
Set m = ms(i)

Digits = m.Value
strFormat = String(m.Length, "0")

Digits = Format(Digits + Incr, strFormat)
If Len(Digits) > m.Length Then
Digits = Mid(Digits, 2)
Else
Incr = 0
End If

Mid(Result, m.FirstIndex + 1, m.Length) = Digits

Next 'm
'--------------------------------
IncNum = Result

If Incr > 0 Then
Err.Raise 5, Err.Source, "Unable to increment code string."
End If
'--------------------------------
End Function

Requires reference to ms regular expressions 1.0 or 5.5

Try
?IncNum("000asd999.999.999")

Anyone keeping count of the ways?

Regards John

qvb jat vke said:
"John Nurick" <[email protected]>
??????:o[email protected]...
On Thu, 1 Sep 2005 15:15:46 -0500, "Malcolm Cook"

The function I coded handles your sequence just fine. input "AH-1-G1"
output "AH-1-G2". n'est pas?

The one thing it doesn't seem to handle is leading zeros, e.g. input
"AB001", output "AB2" (unless you count the trivial case where the
string contains only digits).
 
Hi John,

This line
Digits = Format(Digits + Incr, strFormat)
and its implicit conversions offended me, so I tried a bit of tidying
up:

Function IncNum4(str As String, _
Optional ErrorOnFail As Boolean = False) As String
'--------------------------------
Dim Result As String
Dim re As Object 'VBScript.RegExp
Dim ms As Object 'VBScript.MatchCollection
Dim Digits As String
'--------------------------------
Result = str

Set re = CreateObject("VBScript.RegExp")
re.Pattern = "\d+"
re.Global = True

Set ms = re.Execute(str)
If ms.Count > 0 Then 'a match exists
With ms(ms.Count - 1) 'rightmost match
Digits = Right(Format(CLng(.Value) + 1, _
String(.Length, "0")), .Length)
Mid(Result, .FirstIndex + 1, .Length) = Digits
End With
Else
If ErrorOnFail Then
Err.Raise 5, Err.Source, "Unable to increment string."
End If
End If
IncNum4 = Result

End Function

Rather than let a good thread die how about :-

Function IncNum(str As String) As String
'--------------------------------
IncNum = ""

If Len(str) < 1 Then
Exit Function
End If
'--------------------------------
Dim Result As String

Dim re As New RegExp
Dim ms As MatchCollection
Dim m As Match

Dim Digits As String
Dim Incr As Long
Dim i As Long
'--------------------------------
Result = str

re.Pattern = "([0-9]+)"
re.Global = True
re.IgnoreCase = True
Set ms = re.Execute(Result)

Incr = 1
'--------------------------------
For i = ms.Count - 1 To 0 Step -1
Set m = ms(i)

Digits = m.Value
strFormat = String(m.Length, "0")

Digits = Format(Digits + Incr, strFormat)
If Len(Digits) > m.Length Then
Digits = Mid(Digits, 2)
Else
Incr = 0
End If

Mid(Result, m.FirstIndex + 1, m.Length) = Digits

Next 'm
'--------------------------------
IncNum = Result

If Incr > 0 Then
Err.Raise 5, Err.Source, "Unable to increment code string."
End If
'--------------------------------
End Function

Requires reference to ms regular expressions 1.0 or 5.5

Try
?IncNum("000asd999.999.999")

Anyone keeping count of the ways?

Regards John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top