Finding numbers in strings without regexs?

Started by meredi, August 11, 2008, 08:52:39 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

meredi

Hi Visio Guy & co,

I'm using Visio 2003. I want to take a string from a shape, search for a number at the end of it, and increment the number. (This is all part of a copy page macro. If the page is titled "Page 22" I want to offer up the new title "Page 23" when the user copies the page.)

It seems like it's fairly simple but I tie myself in knots whenever I try to do it, and I can't find any regular expression support in Visio.

Can anyone tell me how I'd go about doing this? Do I really have to do something like "if final character = 1, or if final character = 2, or if final character = 3..."? Surely there's a better way -- maybe a function I haven't found yet?

Grateful for any advice,

Meredi

Visio Guy

Hi Meredi,

Whatcha using?

VBA? VB.NET, C#, Visual COBOL ;)
For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

Paul Herber

I'm assuming that you aren't trying to do this within the shapesheet! Can you not get a RegExp addon for whatever language you are using.


Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

Visio Guy

Without RegEx, it can be fairly simple - something like this:

Dim r as string
r = Right( "I am number 9", 1 )
If InStr(r, "012345689" ) Then
     Debug.WriteLine("Right-most character is a number")
End If
For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

meredi

#4
Oops, sorry, am using VBA.

Thanks for the coding suggestion -- I didn't know the Right function existed, so that's very helpful for starters.

Now the thing I'm curious about is InStr -- can it really work that way? I would have assumed that given your code it would look for the ENTIRE string "0123456789" and return false because that's impossible in a single character string?

Is this just some cool undocumented behaviour? I honestly never would have coded that, given what I'm reading in the help file!

Edit: I'm a moron -- I read the InStr backwards. Honestly never would have thought of turning it around like that. You're asking is my 1-char string anywhere in the string "0123456789". Duh, why didn't I think of that! Thanks Visio Guy.

Visio Guy

I said "it was something like..." meaning I'm not gonna look it up :) :) :)
For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

meredi

#6
Yeah, no worries, I got myself all turned around. Read it right the first time, then thought I got it backwards, etc.

For anyone who's curious, the final function is as follows:


Function CopyPage_DoesPageNameEndInNumber(pageName)

Dim result As Boolean
result = False

If InStr("0123456789", Right(pageName, 1)) Then
     result = True
End If

CopyPage_DoesPageNameEndInNumber = result

End Function


I am going to have to get a bit fancier (the number could be in the double-digits, feasibly) but this is the basic code. Thanks again.