Regular expression
|
|
Thread rating:  |
Brian Schwartz - 29 Dec 2004 15:58 GMT Hi all,
I'm trying to figure out regular expressions. I want to check a string for a negative sign--where one is allowed, but not required, on the front, and not allowed anywhere else. I know I can use (where x = the string I'm checking):
InStr(2, x, "-")
but I'm trying to wrap my head around regular expressions, and want to know how to do it that way. My code is phrased like:
If [invalid string] then 'reset to previous value
So, if x like "*-*" disallows one anywhere if x like "-*-*" disallows them anywhere if one is on the front, but allows them anywhere if one is not on the front. And I'm out of ideas after that.
Thanks, Brian
 Signature Brian Schwartz FishNet Components http://www.fishnetcomponents.com Building better tools for developers - Be part of it!
Veign - 29 Dec 2004 16:31 GMT If you want help in Regular Expression and their usage then check out: http://www.regexlib.com/
-or-- http://www.regexbuddy.com/
What you are doing isn't really Regular Expression as it more of a string expression since you don't have the full power of regular expressions. To get regular expression in VB you need to use the MS Scripting Library.
Sample code for you example: Dim strTest As String strTest = "-AbA"
If Not strTest Like "*-*-*" And Left$(strTest, 1) Like "[-*]" Then Debug.Print "Success" End If
 Signature Chris Hanscom - Microsoft MVP (VB) Veign's Resource Center http://www.veign.com/vrc_main.asp --
> Hi all, > [quoted text clipped - 18 lines] > Thanks, > Brian Rick Rothstein - 29 Dec 2004 17:21 GMT > Sample code for you example: > Dim strTest As String [quoted text clipped - 3 lines] > Debug.Print "Success" > End If The above test isn't testing what you apparently think it is. The second expression will be True only if the leading character of strTest is a minus sign or and asterisk; remove the minus sign from your expression and the If-Then test will fail. Usually, if we are talking about String values that take an optional minus sign at the beginning, the String value is almost always composed of digits (and, if a floating point value, possibly a single decimal point). If digits only, your second expression would become
Left$(strTest, 1) Like "[-0-9]"
If we are talking about floating point values, then things get more complicated as the decimal point could be the first character, or second character following a minus sign and, of course, there can only be one minus sign in the expression. The way I solve this problem is to test for the minus sign independently of the "number proofing" code (this method, by the way, works whether the strTest value is numeric or not). For example,
Dim strTest As String Dim Temp As String ' The Value variable is set from being the argument in a ' Function or Sub, a "global" variable, or whatever. strTest = <Some String> ' Copy strTest to Temp because we are going to ' modify it and we want to preserve the original strTest Temp = strTest If Left$(Temp, 1) = "-" Then Temp = Mid$(Temp, 2) ' Now that the minus sign has been removed, perform ' whatever test to see if the remainder of the ' String value is properly formed at this location ....... .......
Rick - MVP
Veign - 29 Dec 2004 18:50 GMT Good catch Rick. I never went back and tested a string with no minus sign...
 Signature Chris Hanscom - Microsoft MVP (VB) Veign's Resource Center http://www.veign.com/vrc_main.asp --
> > Sample code for you example: > > Dim strTest As String [quoted text clipped - 39 lines] > > Rick - MVP Veign - 29 Dec 2004 19:02 GMT This should now pass his test: If Not strTest Like "*-*-*" And Not InStr(1, strTest, "-") > 1 Then Debug.Print "Success" End If
Should allow: -ABC ABC
Not Allow -A-BC A-BC
 Signature Chris Hanscom - Microsoft MVP (VB) Veign's Resource Center http://www.veign.com/vrc_main.asp --
> > Sample code for you example: > > Dim strTest As String [quoted text clipped - 39 lines] > > Rick - MVP Rick Rothstein - 29 Dec 2004 19:27 GMT > This should now pass his test: > If Not strTest Like "*-*-*" And Not InStr(1, strTest, "-") > 1 Then > Debug.Print "Success" > End If Yes, provided the empty string ("") or a single minus sign (character) are to be considered "successes". Also, if the OP is dealing with floating point numbers (instead of just text), then adjustments will have to be made to his decimal point "proofing" code to allow for "-." at the beginning of the strTest value.
Rick - MVP
Veign - 29 Dec 2004 19:49 GMT Tough to say what the reason or the intent of the check is from the OP...
 Signature Chris Hanscom - Microsoft MVP (VB) Veign's Resource Center http://www.veign.com/vrc_main.asp --
> > This should now pass his test: > > If Not strTest Like "*-*-*" And Not InStr(1, strTest, "-") > 1 Then [quoted text clipped - 8 lines] > > Rick - MVP Brian Schwartz - 29 Dec 2004 23:00 GMT > What you are doing isn't really Regular Expression Thanks for the clarification.
From your and Rick's posts, it's obvious that pattern matching isn't always a great solution for checking strings. I've seen it do some cool things, but for the minus sign, my original
InStr(2, x, "-")
is much better. BTW, the purpose is to create a numeric-only (including floating-point values) textbox, which we've talked about other aspects of before in this newsgroup. I'll post my complete code for everyone in the near future.
Thanks! Brian
 Signature Brian Schwartz FishNet Components http://www.fishnetcomponents.com Building better tools for developers - Be part of it!
> Tough to say what the reason or the intent of the check is from the OP... > [quoted text clipped - 10 lines] > > > > Rick - MVP Veign - 30 Dec 2004 00:48 GMT This is the code I use for numeric only;
Public Function ValidateNumeric(ByVal strText As String) As Boolean
On Error GoTo Hell
' NumValue contains the string to test If strText Like "#" Or (Len(strText) > 1 And Left$(strText, _ 1) Like "[-+.0-9]" And Not Mid$(strText, 2) Like "*[!.0-9]*" And Not _ strText Like "*.*.*") Then ValidateNumeric = True
Exit Function
Hell: ValidateNumeric = False
End Function
 Signature Chris Hanscom - Microsoft MVP (VB) Veign's Resource Center http://www.veign.com/vrc_main.asp --
> > What you are doing isn't really Regular Expression > [quoted text clipped - 28 lines] > > > > > > Rick - MVP Rick Rothstein - 30 Dec 2004 01:10 GMT > > What you are doing isn't really Regular Expression > [quoted text clipped - 10 lines] > before in this newsgroup. I'll post my complete code for everyone in the > near future. Here are two functions that I have posted in the past for similar questions..... one is for digits only and the other is for "regular" numbers:
Function IsDigitsOnly(Value As String) As Boolean IsDigitsOnly = Len(Value) > 0 And _ Not Value Like "*[!0-9]*" End Function
Function IsNumber(ByVal Value As String) As Boolean ' Leave the next statement out if you don't ' want to provide for plus/minus signs If Value Like "[+-]*" Then Value = Mid$(Value, 2) IsNumber = Not Value Like "*[!0-9.]*" And _ Not Value Like "*.*.*" And _ Len(Value) > 0 And Value <> "." And _ Value <> vbNullString End Function
Here are revisions to the above functions that deal with the local settings for decimal points (and thousand's separators) that are different than used in the US.
Function IsNumber(ByVal Value As String) As Boolean Dim DP As String ' Get local setting for decimal point (do not ' modify this statement, it is correct as written) DP = Format$(0, ".") ' Leave the next statement out if you don't ' want to provide for plus/minus signs If Value Like "[+-]*" Then Value = Mid$(Value, 2) IsNumber = Not Value Like "*[!0-9" & DP & "]*" And _ Not Value Like "*" & DP & "*" & DP & "*" And _ Len(Value) > 0 And Value <> DP And _ Value <> vbNullString End Function
I'm not as concerned by the rejection of entries that include one or more thousand's separators, but we can handle this if we don't insist on the thousand's separator being located in the correct positions (in other words, we'll allow the user to include them for their own purposes... we'll just tolerate their presence).
Function IsNumber(ByVal Value As String) As Boolean Dim DP As String Dim TS As String ' Get local setting for decimal point (do not ' modify this statement, it is correct as written) DP = Format$(0, ".") ' Get local setting for thousand's separator ' and eliminate them. Remove the next two lines ' if you don't want your users being able to ' type in the thousands separator at all. TS = Mid$(Format$(1000, "#,###"), 2, 1) Value = Replace$(Value, TS, "") ' Leave the next statement out if you don't ' want to provide for plus/minus signs If Value Like "[+-]*" Then Value = Mid$(Value, 2) IsNumber = Not Value Like "*[!0-9" & DP & "]*" And _ Not Value Like "*" & DP & "*" & DP & "*" And _ Len(Value) > 0 And Value <> DP And _ Value <> vbNullString End Function
Rick - MVP
Brian Schwartz - 31 Dec 2004 23:30 GMT Thanks Rick & Chris, I have that part down. ;) I had something a little different in mind...see my other post this afternoon.
Happy New Year, Brian
 Signature Brian Schwartz FishNet Components http://www.fishnetcomponents.com Building better tools for developers - Be part of it!
> Here are two functions that I have posted in the past for similar > questions..... one is for digits only and the other is for "regular" > numbers:
|
|
|