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.
Printable View
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.
[^123] can be used so that if it starts with 123, no match.
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 ).
Is it always a string of numbers?
yes.
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?
(?!123)
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.
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})
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.
I can't say I'm familiar with ?!, is that effectively the same as ^?
((?!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?
Try
^(?!123)\d*$
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.
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})
All three of us jumped on the answer bandwagon at the same time. =D
Is there an AND in Regular Expressions? What if I want it to not start with 123 and also not start with 789?
^((?!123)(?!789)\d*)$
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....
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...
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?
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*$
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.
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?
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))(.*)/
You are on a .Net forum.Quote:
Originally Posted by penagate
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.
For example of what I mean on the javascript argument, try the following:
assert(/^(?!123)\d*$/.test("123456"));
vs.
assert(/^(?!123)\d*$/.test("456123"));
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" />
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?
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]);
}
Try this one:
^\s*(?!123)(?!0)(.*)$
Does that work?
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.
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)(.*)$