RegExp

C

CJM

I'm trying to validate and input field that allows an integer or a single
'*' character using regular expressions, but I'm not getting the results I'm
expecting.

I'm trying to match against the pattern "[^0-9]*|[^\*]", which I hoped would
translate to 'match against groups of the numbers (0-9) or against a single
*'. However, it's matching against character 1 for each of the examples
below when Global = False. When Global = True, it matches against character
2 for S4/S5, and against one after the last character for S1-S3.

I'm sure this is a simple one for the RegExp experts amongst us. Any
thoughts?

Thansk

CJM



'Code Snippet
Dim oRegExp, oExpMatch, s1, s2, s3, s4, s5

Set oRegExp = new RegExp

With oRegExp
s1 = "12345" 'valid
s2 = "*" 'valid
s3 = "12*" 'not valid
s4 = "ABC" 'not valid
s5 = "**" 'not valid

.Global = True
.Pattern = "[^0-9]*|[^\*]"
Set oExpMatch = oRegExp.Execute(s1)
Set oExpMatch = oRegExp.Execute(s2)
Set oExpMatch = oRegExp.Execute(s3)
Set oExpMatch = oRegExp.Execute(s4)
Set oExpMatch = oRegExp.Execute(s5)

Response.Write "Match " & oExpMatch.Count

End With

Set oExpMatch = nothing
Set oRegExp = nothing
 
T

Tim Slattery

CJM said:
I'm trying to validate and input field that allows an integer or a single
'*' character using regular expressions, but I'm not getting the results I'm
expecting.
I'm trying to match against the pattern "[^0-9]*|[^\*]", which I hoped would
translate to 'match against groups of the numbers (0-9) or against a single
*'.
With oRegExp
s1 = "12345" 'valid
s2 = "*" 'valid
s3 = "12*" 'not valid
s4 = "ABC" 'not valid
s5 = "**" 'not valid

I'm not entirely sure what you're trying to get. I think you want to
match a string made entirely of digits, or a string consisting of a
single *.

You're quite close to that. The first thing you need to do is to
remove the beginning-of-line character (^) from the character classes.
Then specify one or more digits and have it match end-of-line:

"^([0-9]+|\*)$"

beginning of line, then either one or more digits or a single *, then
end of line.
 
C

CJM

Tim Slattery said:
I'm not entirely sure what you're trying to get. I think you want to
match a string made entirely of digits, or a string consisting of a
single *.

Yep, that's right.
You're quite close to that.

Surprising but reassuring!
The first thing you need to do is to
remove the beginning-of-line character (^) from the character classes.
Then specify one or more digits and have it match end-of-line:

Ah... I didn't know about/didn't understand about these end-of-line thingys.
"^([0-9]+|\*)$"

Needless to say, it works - thanks. But I've also broken it down and looked
at the structure, and it also makes sense, which is even better.
beginning of line, then either one or more digits or a single *, then
end of line.

Thanks Tims - very helpful.

CJM
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top