Regular Expression Help

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am in need of a regular expression that tests and fails if there are 14 or
more of a character in the test string. There can be up to 13 of these
characters in the string and any other characters, but at the 14th of this
character it should fail.

Thanks,

Todd Meister
 
Todd,
I'm almost positive you'll be better off going ToCharArray() and looping
through the char array....something like:

public static bool HasTooManyOfASingleCharacter(string source, char
character, int max) {
char[] array = source.ToCharArray();
int count = 0;
for (int i = 0; i < array.Length; ++i ){
char c = array;
if (c == character){
++count;
if (count > max){
return true;
}
}
}
return false;
}
 
Part of the problem is that I have a web application that builds forms
dynamically. I then allow for validation routines for the given controls.
For validation the system allows for multiple regular expressions for each
control if needed and the form. When someone posts their data I loop through
all of the controls and then pull their specific validation requirements from
the database.

Let me know if you know what the regular expression would be to limit X
number of characters in a string.

Thanks

Karl said:
Todd,
I'm almost positive you'll be better off going ToCharArray() and looping
through the char array....something like:

public static bool HasTooManyOfASingleCharacter(string source, char
character, int max) {
char[] array = source.ToCharArray();
int count = 0;
for (int i = 0; i < array.Length; ++i ){
char c = array;
if (c == character){
++count;
if (count > max){
return true;
}
}
}
return false;
}


--
MY ASP.Net tutorials
http://www.openmymind.net/


tmeister said:
I am in need of a regular expression that tests and fails if there are 14 or
more of a character in the test string. There can be up to 13 of these
characters in the string and any other characters, but at the 14th of this
character it should fail.

Thanks,

Todd Meister
 
Just so I'm clear, you want to limit on a certain character...such as 10 'a'
or more and it fails..but not necessarily in a row...so like
abaaaaacasaaaaaadasdsa would fail 'cuz in the totallity of the string there
are more than 10 'a'?

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


tmeister said:
Part of the problem is that I have a web application that builds forms
dynamically. I then allow for validation routines for the given controls.
For validation the system allows for multiple regular expressions for each
control if needed and the form. When someone posts their data I loop through
all of the controls and then pull their specific validation requirements from
the database.

Let me know if you know what the regular expression would be to limit X
number of characters in a string.

Thanks

Karl said:
Todd,
I'm almost positive you'll be better off going ToCharArray() and looping
through the char array....something like:

public static bool HasTooManyOfASingleCharacter(string source, char
character, int max) {
char[] array = source.ToCharArray();
int count = 0;
for (int i = 0; i < array.Length; ++i ){
char c = array;
if (c == character){
++count;
if (count > max){
return true;
}
}
}
return false;
}


--
MY ASP.Net tutorials
http://www.openmymind.net/


tmeister said:
I am in need of a regular expression that tests and fails if there are
14
or
more of a character in the test string. There can be up to 13 of these
characters in the string and any other characters, but at the 14th of this
character it should fail.

Thanks,

Todd Meister
 
What I'm really after is limiting the number of lines that are typed in the
text box.
I started with something like

(.\r\n){1,4}

but this one fails because it looks for, I think, between 1 and 4 carriage
return line feeds. It also works if you have 5 6 7 and so on.

Thanks,

Todd Meister
Karl said:
Just so I'm clear, you want to limit on a certain character...such as 10 'a'
or more and it fails..but not necessarily in a row...so like
abaaaaacasaaaaaadasdsa would fail 'cuz in the totallity of the string there
are more than 10 'a'?

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


tmeister said:
Part of the problem is that I have a web application that builds forms
dynamically. I then allow for validation routines for the given controls.
For validation the system allows for multiple regular expressions for each
control if needed and the form. When someone posts their data I loop through
all of the controls and then pull their specific validation requirements from
the database.

Let me know if you know what the regular expression would be to limit X
number of characters in a string.

Thanks

Karl said:
Todd,
I'm almost positive you'll be better off going ToCharArray() and looping
through the char array....something like:

public static bool HasTooManyOfASingleCharacter(string source, char
character, int max) {
char[] array = source.ToCharArray();
int count = 0;
for (int i = 0; i < array.Length; ++i ){
char c = array;
if (c == character){
++count;
if (count > max){
return true;
}
}
}
return false;
}


--
MY ASP.Net tutorials
http://www.openmymind.net/


I am in need of a regular expression that tests and fails if there are 14
or
more of a character in the test string. There can be up to 13 of these
characters in the string and any other characters, but at the 14th of this
character it should fail.

Thanks,

Todd Meister

 
Back
Top