I have a textbox field, and i want to regulate whats typed in.
I dont want users to type more or less then 6-10 characters. here is my regex for limiting the characters ^.{6,10}$ I got this part working, but I also dont want users to type in whitespace(space). Now i am able to detect whitespace from the beginning of the input and ending of the input, but not if the user types in space in middle of the text. see example.
" testing" = regulator detects the space in the beginning. regex i use ^[^\s].+[^\s]$
"testing " = regulator detects the space in the end. regex i use here is same as abow
"test ing" = regulator does not detect the space in the middle. tried different regex with no luck.
how can i create a regulator which will do all that i require?
It is the problem with your . which matches everything
Do this
^[^\s]{6,10}$
[^\s] matches any character except space
OR
^\w{6,10}$
\w is similar to [\da-zA-Z_]
Some1.Kill.The.DJ answers is great, but for your personnal knowledge, you can also use the following:
^\S{6,10}$
\S matches any characters except space the same way [^\s] does
Related
I know the regex for excluding words, roughly anyway, It would be (!?wordToIgnore|wordToIgnore2|wordToIgnore3)
But I have an existing, complicated regex that I need to add this to, and I am a bit confused about how to go about that. I'm still pretty new to regex, and it took me a very long time to make this particular one, but I'm not sure where to insert it or how ...
The regex I have is ...
^(?!.*[ ]{2})(?!.*[']{2})(?!.*[-]{2})(?:[a-zA-Z0-9 \:/\p{L}'-]{1,64}$)$
This should only allow the person typing to insert between 1 and 64 letters that match that pattern, cannot start with a space, quote, double quote, special character, a dash, an escape character, etc, and only allows a-z both upper and lowercase, can include a space, ":", a dash, and a quote anywhere but the beginning.
But I want to forbid them from using certain words, so I have this list of words that I want to be forbidden, I just cannot figure out how to get that to fit into here.. I tried just pasting the whole .. "block" in, and that didn't work.
?!the|and|or|a|given|some|that|this|then|than
Has anyone encountered this before?
ciel, first off, congratulations for getting this far trying to build your regex rule. If you want to read something detailed about all kinds of exclusions, I suggest you have a look at Match (or replace) a pattern except in situations s1, s2, s3 etc
Next, in your particular situation, here is how we could approach your regex.
For consision, let's make all the negative lookarounds more compact, replacing them with a single (?!.*(?: |-|'){2})
In your character class, the \: just escapes the colon, needlessly so as : is enough. I assume you wanted to add a backslash character, and if so we need to use \\
\p{L} includes [a-zA-Z], so you can drop [a-zA-Z]. But are you sure you want to match all letters in any script? (Thai etc). If so, remember to set the u flag after the regex string.
For your "bad word exclusion" applying to the whole string, place it at the same position as the other lookarounds, i.e., at the head of the string, but using the .* as in your other exclusions: (?!.*(?:wordToIgnore|wordToIgnore2|wordToIgnore3)) It does not matter which lookahead comes first because lookarounds do not change your position in the string. For more on this, see Mastering Lookahead and Lookbehind
This gives us this glorious regex (I added the case-insensitive flag):
^(?i)(?!.*(?:wordToIgnore|wordToIgnore2|wordToIgnore3))(?!.*(?: |-|'){2})(?:[\\0-9 :/\p{L}'-]{1,64}$)$
Of course if you don't want unicode letters, replace \p{L} with a-z
Also, if you want to make sure that the wordToIgnore is a real word, as opposed to an embedded string (for instance you don't want cat but you are okay with catalog), add boundaries to the lookahead rule: (?!.*\b(?:wordToIgnore|wordToIgnore2|wordToIgnore3)\b)
use this:
^(?!.*(the|and|or|a|given|some|that|this|then|than))(?!.*[ ]{2})(?!.*[']{2})(?!.*[-]{2})(?:[a-zA-Z0-9 \:\p{L}'-]{1,64}$)$
see demo
I use this ^((\d-\d{3})|(\d{1,3}))\-\d{3}-\d{7}$ regex to validate phone numbers.
However it does not accept the following number which is valid. What is the problem?
Could it have to do with the zeros at the end?
90-312-2488900
Your regexp seems to work properly.
Maybe the problem is in the last space in your example. Try to remove it with string.Trim, or add \s* to your regexp (or even add it between every groups of numbers.
The space at the end of your example is what is causing it to not match. Go to regexpal
and type in your regex. When you try your example, it will show you what matches and what doesn't. Without the space, you are good to go. As JleruOHep recommended, try trimming the string, or allow whitespace in your regex.
Here is your phone number without whitespace at the end.
And with whitespace.
The yellow highlight is what matches.
There is a long set of characters that are not allowed to validate an input box of winform app.
So i figured that rather than making the long list that are not allowed make the shorter one that are allowed.
The set that is allowed are (a-z,A-Z, 0-9,#,.) .Rest every thing that can be entered are not allowed.
This is the regex that i have made for this.
Regex.IsMatch(textBox1.Text, #"[#\.\w]+$")
It seem to work in some cases but when i enter the data in this format normal character or number special character normal character or number it seems to break few example ee(vv, 55)44,aba&3B.
Not able to figure out whats wrong in this.
Your regex is not valid, because you don't validate all string, but the last part.
You should start it with ^ - beginning of the line symbol.
Regex.IsMatch(textBox1.Text, #"^[\w#.]*$")
\w also means letters in every language, so it will validate the string "абц" too.
So if you need only for english, use
Regex.IsMatch(textBox1.Text, #"^[a-zA-Z0-9#.]*$")
Try this :
Regex.IsMatch(textBox1.Text, #"^[a-zA-Z0-9#.]*$")
Use
^[-a-zA-Z0-9 _ - \. #]*
as the Regex expression text.
I have the following regular expression which specifies characters the user is allowed to type:
#"^[A-Za-z0-9/!\$%\^&\*\(\)\-_\+\[\]\{\}\;\:\'\£\#\#\.\?]*$
I know '\s' is the character class for white space, but how do I add this to the regular expression so it excludes it? I have searched for this on Stack Overflow - the questions provide solutions to exclude white space but not how to use it in an existing regular expression. If I add it like I have the other characters, it would mean 'allow white space'?
Edit: Not sure why this has been marked down? Thanks to everyone for their answers
First of all, you don't need all those escapes.
Second of all, the regex already doesn't allow whitespaces.
#"^[A-Za-z0-9\[\]/!$%^&*()\-_+{};:'£##.?]*$"
The [] define a set of characters to allow (followed by * means that characters from the characters set can be zero or more times).
^ matches the beginning and $ matches the end, so the fact that /s isn't anywhere there, means white spaces won't be allowed.
In regex ^ within the square brackets indicates negation so adding ^\s would mean "not whitespace."
In the regular expression, if you don't write "\s". This means your regular expression doesn't allow any blank spaces.
For example:
1) If I don't want to allow any user to enter blank space in the text box, then the regular expression is written as:
Regex alphabet=new Regex(#"^[a-z]*$");
2) If the user is allowed to enter any blank space in the text box then the regular expression is written as:
Regex alphabet=new Regex(#"^[a-z\s]*$");
3) In regular expression " ^ " means beginning and " $ " means end.
NOTE: ^\s means negation of "\s"
Regex regfname = new Regex(#"^[^\s][a-zA-Z]*$");
this code will not allow any blank spaces and no empty text box!
I hope this will help you to understand the concept of blank spaces in regular expression..
Basically, the input field is just a string. People input their phone number in various formats. I need a regular expression to find and convert those numbers into links.
Input examples:
(201) 555-1212
(201)555-1212
201-555-1212
555-1212
Here's what I want:
(201) 555-1212 - Notice the space is gone
(201)555-1212
201-555-1212
555-1212
I know it should be more robust than just removing spaces, but it is for an internal web site that my employees will be accessing from their iPhone. So, I'm willing to "just get it working."
Here's what I have so far in C# (which should show you how little I know about regular expressions):
strchk = Regex.Replace(strchk, #"\b([\d{3}\-\d{4}|\d{3}\-\d{3}\-\d{4}|\(\d{3}\)\d{3}\-\d{4}])\b", "<a href='tel:$&'>$&</a>", RegexOptions.IgnoreCase);
Can anyone help me by fixing this or suggesting a better way to do this?
EDIT:
Thanks everyone. Here's what I've got so far:
strchk = Regex.Replace(strchk, #"\b(\d{3}[-\.\s]\d{3}[-\.\s]\d{4}|\(\d{3}\)\s*\d{3}[-\.\s]\d{4}|\d{3}[-\.\s]\d{4})\b", "<a href='tel:$1'>$1</a>", RegexOptions.IgnoreCase);
It is picking up just about everything EXCEPT those with (nnn) area codes, with or without spaces between it and the 7 digit number. It does pick up the 7 digit number and link it that way. However, if the area code is specified it doesn't get matched. Any idea what I'm doing wrong?
Second Edit:
Got it working now. All I did was remove the \b from the start of the string.
Remove the [] and add \s* (zero or more whitespace characters) around each \-.
Also, you don't need to escape the -. (You can take out the \ from \-)
Explanation: [abcA-Z] is a character group, which matches a, b, c, or any character between A and Z.
It's not what you're trying to do.
Edits
In response to your updated regex:
Change [-\.\s] to [-\.\s]+ to match one or more of any of those characters (eg, a - with spaces around it)
The problem is that \b doesn't match the boundary between a space and a (.
Afaik, no phone enters the other characters, so why not replace [^0-9] with '' ?
Here's a regex I wrote for finding phone numbers:
(\+?\d[-\.\s]?)?(\(\d{3}\)\s?|\d{3}[-\.\s]?)\d{3}[-\.\s]?\d{4}
It's pretty flexible... allows a variety of formats.
Then, instead of killing yourself trying to replace it w/out spaces using a bunch of back references, instead pass the match to a function and just strip the spaces as you wanted.
C#/.net should have a method that allows a function as the replace argument...
Edit: They call it a `MatchEvaluator. That example uses a delegate, but I'm pretty sure you could use the slightly less verbose
(m) => m.Value.Replace(' ', '')
or something. working from memory here.