|
-
Mar 3rd, 2006, 10:18 AM
#1
Thread Starter
Fanatic Member
Regular Expression [ RESOLVED ]
I'm trying to add a regular expression that does not match
if the string begins 123. How can I do this? Any other number beginning will
be a match, such as 133, 124, etc.
Last edited by dogboydelta; Mar 6th, 2006 at 04:47 PM.
-
Mar 3rd, 2006, 10:25 AM
#2
Re: Regular Expression
[^123] can be used so that if it starts with 123, no match.
-
Mar 3rd, 2006, 10:39 AM
#3
Thread Starter
Fanatic Member
Re: Regular Expression
That's what I tried first, but [^123] matches not any of the characters, so if the
string starts with a 1, 2 or 3 it will be rejected. The [^...] only matches one character. I need to reject a string that starts exactly 123. If it starts 321 or 132 it should be accepted. I tried [^1][^2][^3] but that didn't work either, as it matches 3 characters, but if any one is out of line it will reject ( ie. 943 would be rejected ).
-
Mar 3rd, 2006, 10:42 AM
#4
Re: Regular Expression
Is it always a string of numbers?
-
Mar 3rd, 2006, 11:17 AM
#5
Thread Starter
Fanatic Member
-
Mar 3rd, 2006, 11:31 AM
#6
Re: Regular Expression
I'll try to figure out the regex for this, but couldn't you just use a simple string.IndexOf() operation to figure out whether the first three characters are 123?
-
Mar 3rd, 2006, 11:32 AM
#7
Need to re-register ASP.NET?
C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i
(Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)
-
Mar 3rd, 2006, 11:34 AM
#8
Re: Regular Expression
Well, at least that's it in theory but now that I test it - it doesn't seem to work in practice. Gimme a min. I'll get it for you.
Need to re-register ASP.NET?
C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i
(Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)
-
Mar 3rd, 2006, 11:37 AM
#9
Re: Regular Expression
OK, the dot-net syntax is slightly different than I am used to, but here is what it works out to be:
((?!123)\d{3})
Need to re-register ASP.NET?
C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i
(Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)
-
Mar 4th, 2006, 03:50 PM
#10
Thread Starter
Fanatic Member
Re: Regular Expression
Its probably going to be used in a RegularExpressionValidator ( client side ) aspx file, so it pretty much has to be a straight regularexpression, cant use any string methods. I will try that latest regex and see what happens.
-
Mar 4th, 2006, 06:08 PM
#11
Re: Regular Expression
I can't say I'm familiar with ?!, is that effectively the same as ^?
-
Mar 6th, 2006, 11:54 AM
#12
Thread Starter
Fanatic Member
Re: Regular Expression
((?!123)\d{3})
I tried this one - its on the right track. It accepted 124 and rejected 123. The problem is it accepted 123456. Any other suggestions?
-
Mar 6th, 2006, 12:07 PM
#13
-
Mar 6th, 2006, 12:11 PM
#14
Re: Regular Expression
OK, I was thinking it was only a three digit field.
What you are looking for then is:
^((?!123)\d*)
Mendhak:
(?! means "check what is coming up next and make sure it does NOT match this pattern"
(^ is not something I am familiar with.
Need to re-register ASP.NET?
C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i
(Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)
-
Mar 6th, 2006, 12:14 PM
#15
Thread Starter
Fanatic Member
Re: Regular Expression
I got it, you have to add the begin match ^ at the beginning ( otherwise you can match "NOT 123" anywhere in the string and it will be a match.
This works:
^((?!123)\d{3})
-
Mar 6th, 2006, 12:18 PM
#16
Re: Regular Expression
All three of us jumped on the answer bandwagon at the same time. =D
Need to re-register ASP.NET?
C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i
(Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)
-
Mar 6th, 2006, 12:19 PM
#17
Thread Starter
Fanatic Member
Re: Regular Expression
Is there an AND in Regular Expressions? What if I want it to not start with 123 and also not start with 789?
-
Mar 6th, 2006, 12:20 PM
#18
Need to re-register ASP.NET?
C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i
(Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)
-
Mar 6th, 2006, 12:25 PM
#19
Re: Regular Expression
I understand ?! now. I was using ^, which is apparently regular-regular expressions, but .NET probably confuses it with the anchor start of the string, so it expects ?!.
Fuhh....
-
Mar 6th, 2006, 12:30 PM
#20
Re: Regular Expression
Well, ?! is pretty standard really. =)
But hey, Regular Expressions themselves are for people who are nutjobs (like us) to begin with. I mean, look at what we just wrote and the fact is even makes sense to someone...
Need to re-register ASP.NET?
C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i
(Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)
-
Mar 6th, 2006, 12:44 PM
#21
Thread Starter
Fanatic Member
Re: Regular Expression
Now I'm having another problem. ^((?!123)\d{3}) works great when I test it
using the Regex methods in vb, but when I plug it into a regularexpression validator, it rejects any number I type in at all. What's the deal? I'm using ASP.NET 1.1 , is there some compatibility issue?
-
Mar 6th, 2006, 12:48 PM
#22
Re: Regular Expression
No, if there is anything it is the validator itself since to test my regular expressions I use VB.Net 1.1 using a program I wrote myself.
Try taking out the outside set of parens:
^(?!123)\d*$
Need to re-register ASP.NET?
C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i
(Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)
-
Mar 6th, 2006, 01:42 PM
#23
Thread Starter
Fanatic Member
Re: Regular Expression
I have a program too. I try it in the program and it works, then I cut and paste the expression into the web site and it doesn't work - it rejects everything. There are two regularexpression validators on the same textbox, is that a problem? I'm just guessing at this point, I have no idea why it wont work in the Regex Validator.
-
Mar 6th, 2006, 02:16 PM
#24
Thread Starter
Fanatic Member
Re: Regular Expression
Oh I just realized - Validators get converted into Javascript, which may be slightly different as far as Regular Expression syntax compared to .NET, isn't that right?
-
Mar 6th, 2006, 02:19 PM
#25
Re: Regular Expression
Do you need JS regex code or .NET crap? If the former I can help.
If you just want it to match any string that does not start with "123", something like this shoudl work:
/(^(123))(.*)/
Last edited by penagate; Mar 6th, 2006 at 03:18 PM.
Reason: Merged posts.
-
Mar 6th, 2006, 03:07 PM
#26
Re: Regular Expression
 Originally Posted by penagate
... or .NET crap? ...
You are on a .Net forum.
Most of us swear by this language. Be careful.
As for the question, there is no difference between the ASP.Net version and the Javascript version.
The "/" that was just presented is part of the Javascript language, not part of the Regular expression.
Need to re-register ASP.NET?
C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i
(Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)
-
Mar 6th, 2006, 03:18 PM
#27
Re: Regular Expression
For example of what I mean on the javascript argument, try the following:
assert(/^(?!123)\d*$/.test("123456"));
vs.
assert(/^(?!123)\d*$/.test("456123"));
Last edited by Lord_Rat; Mar 6th, 2006 at 03:22 PM.
Need to re-register ASP.NET?
C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i
(Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)
-
Mar 6th, 2006, 03:25 PM
#28
Re: Regular Expression
Forgot that assert is my own function. Here would be the entire code to test:
Code:
<script lang="javascript">
function assert(testVal)
{
if(!testVal) alert("Assert failed!");
}
function A1(){
assert(/^(?!123)\d*$/.test("123456"));
}
function A2(){
assert(/^(?!123)\d*$/.test("456123"));
}
</script>
<input type="button" onclick="A1();" value="1" />
<input type="button" onclick="A2();" value="2" />
Need to re-register ASP.NET?
C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i
(Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)
-
Mar 6th, 2006, 04:24 PM
#29
Thread Starter
Fanatic Member
Re: Regular Expression
Ok, here's my regex:
^((?!\s*\(?\s*123)(?!\s*\(?\s*0))
It rejects a string that starts 123 , OR starts 0, and accepts all others. It works when I do it manually, in Javascript and in .NET Regex( ) methods, but when I plug it into a RegularExpressionValidator it rejects ALL strings. Can someone try that - plug it into a regularexpressionvalidator in ASP.NET 1.1 and see if I'm crazy or not?
-
Mar 6th, 2006, 04:27 PM
#30
Thread Starter
Fanatic Member
Re: Regular Expression
This is what is in the WebUIValidation, in case it helps:
function RegularExpressionValidatorEvaluateIsValid(val) {
var value = ValidatorGetValue(val.controltovalidate);
if (ValidatorTrim(value).length == 0)
return true;
var rx = new RegExp(val.validationexpression);
var matches = rx.exec(value);
return (matches != null && value == matches[0]);
}
-
Mar 6th, 2006, 04:44 PM
#31
Re: Regular Expression
Try this one:
^\s*(?!123)(?!0)(.*)$
Does that work?
Need to re-register ASP.NET?
C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i
(Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)
-
Mar 6th, 2006, 04:47 PM
#32
Thread Starter
Fanatic Member
Re: Regular Expression
Yes it does. I don't know why the WebUIValidation uses RegExp.exec instead of
RegExp.test, which just returns a true or false. Anyway, works good.
-
Mar 6th, 2006, 04:50 PM
#33
Re: Regular Expression [ RESOLVED ]
OK.
One thing I did notice with the expression you pasted before was that it wasn't closed properly. There should have been four ")" at the end.
But glad it works. It might need a set of parens around the \s* part too. I wasn't sure.
Enter a few spaces before your number and then a valid number. If it fails (with a valid number) then that is probably why.
Which would make it:
^(\s*)(?!123)(?!0)(.*)$
Need to re-register ASP.NET?
C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i
(Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|