Regex for mobile number with special characters in c# - c#

I'm trying to build a regular expression to validate mobile numbers that accepts (),- and +. I am totally new to this and this is what I could build
\+?\(?([0-9])\)?[- ]?\(?([0-9])\)?
But I know this wrong since it is accepting data such as 123456- which is not right.
Can somebody please help me build a regex so that itdoes not accept inputs like
123)3575
12349-
-2345678
There is no limit to the number of digits entered. I'm expecting it to accept inputs of type
(123)4567-67898
+234567890
1234-4567-67
Please help..

Something like
/^(\+|\(\d+\))?\d+(-\d+)*$/
Regex Demo
Test
Regex.IsMatch("(123)4567-67898", #"^(\+|\(\d+\))?\d+(-\d+)*$");
=> True
Regex.IsMatch("-2345678", #"^(\+|\(\d+\))?\d+(-\d+)*$");
=> False

Related

c# string format validate

Update: The acceptable format is ADD|| .
I need to check if the request that the server gets, is in this format, and the numbers are between <>.
After that I have to read the numbers and add them and write the result back. So, if the format not fits to for example ADD|<5>|<8>
I have to refuse it and make a specific error message(it is not a number, it is wrong format, etc.). I checked the ADD| part, I took them in an array, and I can check, if the numbers are not numbers. But I cannot check if the numbers are in <> or not, because the numbers can contain multiple digits and ADD|<7>|<13> is not the same number of items likeADD|<2358>|<78961156>. How can I check that the numbers are in between <>?
please help me with the following: I need to make a server-client console application, and I would like to validate requests from the clients. The acceptable format is XXX|<number>|<number>.
I can split the message like here:
string[] messageProcess = message.Split('|');
and I can check if it is a number or not:
if (!(double.TryParse(messageProcess[1], out double number1)) || !(double.TryParse(messageProcess[2], out double number2)))
but how can I check the <number> part?
Thank you for your advice.
You can use Regex for that.
If I understood you correctly, follwing inputs should pass validation:
xxx|1232|32133
xxx|5345|23423
XXX|1323|45645
and following shouldn't:
YYY|1231|34423
XXX|ds12|sda43
If my assumptions are correct, this Regex should do the trick:
XXX\|\d+\|\d+
What it does?
first it looks for three X's... (if it doesn't matter if it's uppercase or lowercase X substitute XXX with (?:XXX|xxx) or use "case insensitive regex flag" - demo)
separated by pipe (|)...
then looks for more than one digit...
separated by pipe (|)...
finally ending with another set of one or more digits
You can see the demo here: Regex101 Demo
And since you are using C#, the Regex.IsMatch() would probably fit you best. You can read about it here, if you are unfamiliar with regular expressions and how to use them in C#.

string to double validation c# please answer

ok so I am building a program in WPF format.
as you know wpf's inputs are usually string, to turn those into double first I need to validate if those string fit and then to proceed and convert them.
the problem is in the validation, I have done the part in the validation that is checking if the string.IsNullOrEmpty but the thing I could not do is validate if the answer is completely not convertable... let me show an example because some strings that are not completely numeric are still should be accepted for example:
"sadasdaasd" - not accepted (obviously)
"8945a4554" - not accepted (there is an 'a' in the middle)
"3519" - accepted
"12.55" - accepted
"-3/4" - accepted and the value should be converted to double as (-3) divided by (4). so '/' is accepted and it splits the string by 2 and then converts it to double as first part/ second part.
I have been trying to do this validation all day and still have not succeeded, I have tried searching the web for some input validation, some said that I need to use double.TryParse(string, out double) but this function does not work with the '/' split that i wanted. so please help me!!!
I would start by parsing your string via regex (q: is "-3*4" acceptable as -3 times 4?). Basically you're looking for a match on a regex which is kind of like this (this works on -3/4, you'd want to test it further and modify if multiplication is allowed): -?\d+[/]\d+
If you find that match, parse out your string with string.Split('/') which will give you an array of strings. TryParse each of those and do the math.
If there is not a match, use TryParse (as recommended previously). That will either succeed (3519, 12.55 in your examples) or fail (sadasdaasd, 8945a4554 in your examples).
Note: you could also use string.Contains('/'), but then you have to check to see if it holds more than one slash (unless such a thing is allowed- in which case you'll need to revisit that regex).

Regex c# to jquery implementation

I have this regex on my asp.net mvc3 application:
Regex pattern = new Regex(#"^(?!.*(.)\1\1)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,20}$");
I needed to implement this with jquery due to some requirements with something like this:
password.match(/(.*(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]/))
This is working. It will detect if 1 uppercase, 1 lowercase and 1 number is present on the password. However , i also have need to detect if 3 consecutive letter is present (eg: aaa, bbb).
With my regex on c# , it is working with the help of:
/(.)\1\1/
But I can't make it work on password.match(/(.)\1\1/)
Did I missed something here? Thanks in advance!
I've just copied your C# regex and tried in JavaScript console and it works great:
"waweEEad2".match(/^(?!.*(.)\1\1)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,20}$/)
returns ["waweEEad2", undefined] and
"waweEEEad2".match(/^(?!.*(.)\1\1)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,20}$/)
returns null.

regular expression validtor

i have text box for phone number .i need to validate it.my requiremants are
Take only numeric more than 10digits
Take symbols like (,),-,
can any one help for this.i tried
^[\d{10,14} +\s +\( +\)-]+$
but not working.
You may take a look at the following article which will help you build such expression.
You haven't said what is wrong with your regex (why it's not working as expected) but I'm guessing that the issue is it matches far more than it should. I.e it will match 1 or more of all the characters in your set (rather than just between 10 and 14).
I think you're mistake is that you have put way too much in your character set. You've got the + symbol in there 3 times and it looks like your trying to use quantifiers from within the set as well, which is not allowed. Character sets are the equivalent of single character alternations. So, [abc] is the equivalent of a|b|c.
I'm assuming that you want the input to be between 10 and 14 numbers while still allowing any number (zero or more) of the following characters:
+()-,
As some others have suggested, you could just put the chars you want in a set and then specify the quantifier after it like this: ^[0-9()-,+]{10,14}$. This will almost get you there. Only problem with it is that it will allow between 10 and 14 of any of these characters, so it would successfully match this:
,,,,,++()---
Which clearly you don't want (do you?)
So, in order to better solve this problem, you'll need to be more specific about what is allowed and where in the subject it is allowed. Because i don't know exactly what you want to match, i can't take you much further.
Hopefully the information I've provided here should be good enough to get you started, and if you have more questions... well that's what we're all here for right, so ask away.
To help you out with learning, below are a few resources you might find useful (this is a small subset of what's available, so do go ahead and search for yourself):
Testing tools
Rubular (ruby)
GSkinner Regex Testser
RegexHero (dotnet)
Helpful info
Regular-Expressions.Info
Codeproject 30 Minute Tutorial

How do I fix this regular expression?

I have the string:
CN=Help & Technical,CN=Users,DC=dave,DC=com
And I want to strip out everything between the '=' and the ',' in a set of groups. Basically Im using this...
=([\w-\s]*)
And it is only dragging back the following :
=help
=users
=dave
So you can see im not getting Help & Technical in the first group which is what I want. Is this possible can anybody help me with the regex I just cant work it out...
I haven't tested this, but =([^,]*) should work.
You just need to include the & sign in your regular expression here.
=([\w-\s&]*)
Note that this is pretty restrictive so far... no apostrophes, no numbers, and no other punctuation. You may want to consider whether any of that will show up and add them as appropriate.
This should work
=(.+),|\w
It should match everything after the = until a , or a enter

Categories