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.
Related
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 am looking for a way to get words out of a sentence. I am pretty far with the following expression:
\b([a-zA-Z]+?)\b
but there are some occurrences that it counts a word when I want it not to. E.g a word followed by more than one period like "text..". So, in my regex I want to have the period to be at the end of a word zero or one time. Inserting \.? did not do the trick, and variations on this have not yielded anything fruitful either.
Hope someone can help!
A single dot means any character. You must escape it as
\.?
Maybe you want an expression like this:
\w+\.?
or
\p{L}+\.?
You need to add \.? (and not .?) because the period has special meaning in regexes.
to avoid a match on your example "test.." you ask for you not only need to put the \.? for checking first character after the word to be a dot but also look one character further to check the second character after the word.
I did end up with something like this
\w{2,}\.?[^.]
You should also consider that a sentence not always ends with a . but also ! or ? and alike.
I usually use rubulator.com to quick test a regexp
Maybe this is a very rare (or even dumb) question, but I do need it in my app.
How can I check if a C# regular expression is trying to match 1-character strings?
That means, I only allow the users to search 1-character strings. If the user is trying to search multi-character strings, an error message will be displaying to the users.
Did I make myself clear?
Thanks.
Peter
P.S.: I saw an answer about calculating the final matched strings' length, but for some unknown reason, the answer is gone.
I thought it for a while, I think calculating the final matched strings length is okay, though it's gonna be kind of slow.
Yet, the original question is very rare and tedious.
a regexp would be .{1}
This will allow any char though. if you only want alpanumeric then you can use [a-z0-9]{1} or shorthand /w{1}
Another option its to limit the number of chars a user can type in an input field. set a maxlength on it.
Yet another option is to save the forms input field to a char and not a string although you may need some handling around this to prevent errors.
Why not use maxlength and save to a char.
You can look for unescaped *, +, {}, ? etc. and count the number of characters (don't forget to flatten the [] as one character).
Basically you have to parse your regex.
Instead of validating the regular expression, which could be complicated, you could apply it only on single characters instead of the whole string.
If this is not possible, you may want to limit the possibilities of regular expression to some certain features. For instance the user can only enter characters to match or characters to exclude. Then you build up the regex in your code.
eg:
ABC matches [ABC]
^ABC matches [^ABC]
A-Z matches [A-Z]
# matches [0-9]
\w matches \w
AB#x-z matches [AB]|[0-9]|[x-z]|\w
which cases do you need to support?
This would be somewhat easy to parse and validate.
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.
This is almost certainly something really silly that I've overlooked but I'm stumped. The following C# style expression is supposed to match phones numbers (a limited subset of them, but this is just testing...):
^[0-9]{3}-[0-9]{3}-[0-9]{4}$
The search string is as follows:
978-454-0586\r\nother junk\r\nmore junk\r\nhttp://www.google.com\r\n
The expression matches the phone number when in isolation, however not when next to other stuff. For example, if I lop off everything after the phone it works just great.
How can I modify the expression so that it matches the phone number and doesn't get hung up on the rest of the junk?
Thanks!
The ^ and $ symbols mean "beginning of line" and "end of line" respectively. Get rid of them if you want to match in the middle of a line.
"$" in a regular expression matches the end of a line. If you remove that, the regexp should work correctly, though if you have "Foo978-454-0586", it won't work, since "^" matches the start of a line.
Are the phone numbers always on their own lines? If so add RegexOptions.Multiline to your Regex constructor. Without that the regex.match is using the beginning and end of the string for ^ and $.
The $ means end of string, not end of line.
The problem is that "^" and "$" forces it to only match on the start of the string and the end of the string.
Remove those two tags and see how you go.