This question already has answers here:
How to validate phone numbers using regex
(43 answers)
Closed 2 years ago.
I need a regex to validate phone number without plus (+) sign for example
46123456789,46-123-456-789,46-123-456-789
number should be 11 digit rest of should ignore
i am currently using this Regex /([+]?\d{1,2}[.-\s]?)?(\d{3}[.-]?){2}\d{4}/g
its not correct at all
About the pattern you tried:
Using this part in your pattern [+]? optionally matches a plus sign. It is wrapped in an optional group ([+]?\d{1,2}[.-\s]?)? possibly also matching 12 digits in total.
The character class [.-\s] matches 1 of the listed characters, allowing for mixed delimiters like 333-333.3333
You are not using anchors, and can also possible get partial matches.
You could use an alternation | to match either the pattern with the hyphens and digits or match only 11 digits.
^(?:\d{2}-\d{3}-\d{3}-\d{3}|\d{11})$
^ Start of string
(?: Non capture group for the alternation
\d{2}-\d{3}-\d{3}-\d{3} Match either the number of digits separated by a hyphen
| Or
\d{11} Match 11 digits
) Close group
$ End of string.
Regex demo
If you want multiple delimiters which have to be consistent, you could use a capturing group with a backreference \1
^(?:\d{2}([-.])\d{3}\1\d{3}\1\d{3}|\d{11})$
Regex demo
I would have this function return true or false and use as is.
function isPhoneValid(phone) {
let onlyNumbers = phone.replace(/[^0-9]/g, "");
if (onlyNumbers.length != 11) console.log(phone + ' is invalid');
else console.log(phone + ' is valid');
}
isPhoneValid('1 (888) 555-1234');
isPhoneValid('(888) 555-1234');
I am not sure how is the input looks like. But based on your question I supposed you want to trim it and match it with regex?
trim your input.
string.split(/[^0-9.]/).join('');
and you can match it with this regex:
((\([0-9]{3}\))|[0-9]{3})[\s\-]?[\0-9]{3}[\s\-]?[0-9]{4}$
Related
Hello I need to exclude sequence of digits from 890000 till 890001;
890002 to 899999 is acceptable
Is it possible doing using regular expression?
No need for regex.
If Value >= 890002 And Value <= 899999 Then
' Accept
End If
Ok, if you insist on using regex (may be for learning purpose):
In this simple case it is actually easier to exclude those two number and match the rest:
^89(?!000[12])\d{4}$
Explanation:
^ match from start of text
89match 89
(?!000[12]) negative look ahead for 3 times zero and one of characters in the character group (1 or 2). If this doesn't block the match:
\d{4} match 4 digits
$ match end of text.
I am trying to create a basic regular expression to match a phone number which can either use dots [.] or hyphens [-] as the separator.
The format is 123.456.7890 or 123-456-7890.
The expression I am currently using is:
\d\d\d[-.]\d\d\d[-.]\d\d\d\d
The issue here is that it also matches the phone numbers that have both separators in them which I want to be termed as invalid/not a match. For example, with my expression, 123.456-7890 and 123-456.7890 show up as a match, something I do not want happening.
Is there a way to do that?
Use a backreference:
^\d{3}([.-])\d{3}\1\d{4}$
Here is an explanation of the regex:
^ from the start of the number
\d{3} match any 3 digits
([.-]) then match AND capture either a dot or a dash separator
\d{3} match any 3 digits
\1 match the SAME separator seen earlier
\d{4} match any 4 digits
$ end of the number
You can use this regex:
^\d{3}([-.])\d{3}\1\d{4}$
You can see that it works here.
Key point here - is that you capture your desired character using brackets ([-.])
and then reuse it with back reference \1.
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I'm trying to develop a regex with the following rules:
it should accept solely numbers,
if the string contains any letters or any other special characters, the whole string should be rejected,
regarding spaces, there should only be one consecutive number group, which can be surrounded by spaces,
if there are more than one consecutive number group, with spaces in between the groups, that whole string should be rejected.
Example Cases:
accepted:
1234
[SPACE][SPACE]111[SPACE]
[SPACE]111[SPACE][SPACE]
declined:
1a234
aa1234aa
1234a
12#4
[SPACE]11[SPACE]111
[SPACE]11[SPACE]111#
So far, I've come up with this ([0-9]+[^\s]*) which can be seen here.
What modifications do I have to do to achieve the scenario I want above?
Use this:
^\s*\d+\s*$
All we need to do is accept one or more digits bounded by zero or more spaces on either side.
EDIT:
Just add a capturing group around the digits to use them later:
^\s*(\d+)\s*$
Demo
The pattern you tried ([0-9]+[^\s]*) matches 1+ digits and 0+ times a non whitespace character using a negated character class [^\s]* matching any character except a whitespace char (So it would match aa)
It can match multiple times in the same string as there are no anchors asserting the start ^ and the end $ of the string.
If you want to match spaces, instead of matching \s which could also match newlines, you could match a single space and repeat that 0+ times on the left and on the right side.
^ *[0-9]+ *$
Regex demo
If you only need the digits, you could use a capturing group
^ *([0-9]+) *$
Regex demo
^\s*[0-9]+\s*$
notice that I've used [0-9] instead of \d
[0-9] will accept only Arabic number (Western Arabic Number)
\d may accept all form of digit in unicode like Eastern Arabic Number, Thai,...etc like (١,٢,٣, ๑,๒,๓, ...etc) at least this is the case in XSD regex when its validate XML file.
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 6 years ago.
I'm trying to figure out how to write a regex that will match every charter up to, but not including the first number in the character sequence if there is one.
Ex:
Input: abc123
Output: abc
Input: #$%##<>#<123
Output: #$%##<>#<
Input: abc
Output: abc
Input: abc #####-122
Output: abc #####-
You can use:
/^([^\d\n]+)\d*.*$/gm
This will also handle scenarios where you have multiple sets of numbers in a string. Example here.
Explanation:
^ # define the start of the stirng
( # open capture group
[^\d\n]+ # match anything that isn't a digit or a newline that occurs once or more
) # close capture group
\d* # zero or more digits
.* # anything zero or more times
$ # define the end of the string
g # global
m # multi line
The greedy matching will mean that by default you will match the capture group and stop capturing as soon as either a digit or anything that isn't matched in the capture group or the end of the string it encountered.
[Update] Try this regex:
([^0-9\n]+)[0-9]?.*
Regex explains:
( capturing group starts
[^0-9\n] match a single character other than numbers and new line
+ match one or more times
) capturing group ends
[0-9] match a single digit number (0-9)
? match zero or more times
.* if any, match all other than new line
Thanks #Robbie Averill for clarifying OP's requirement. Here is the demo.
I did not select a correct answer because the correct answer was left in the comments. "^\D+"
I am working in java, so putting it all together I got:
Pattern p = Pattern.compile("^\\D+");
Matcher m = p.matcher("Testing123Testing");
String extracted = m.group(1);
Use the character class feature: [...]
Identify numbers: [0-9]
Negate the class: [^0-9]
Allow as many as you like: [^0-9]*
This question already has answers here:
Regex to allow numbers and only one hyphen in the middle
(3 answers)
Closed last year.
I want to match a string like 19740103-0379 or 197401030379, i.e the dash is optional.
How do I accomplish this with regexp?
Usually you can just use -?. Alternatively, you can use -{0,1} but you should find that ? for "zero or one occurrences of" is supported just about everywhere.
pax> echo 19740103-0379 | egrep '19740103\-?0379'
19740103-0379
pax> echo 197401030379 | egrep '19740103\-?0379'
197401030379
If you want to accept 12 digits with any number of dashes in there anywhere, you might have to do something like:
-*([0-9]-*){12}
which is basically zero or more dashes followed by 12 occurrences of (a digit followed by zero or more dashes) and will capture all sorts of wonderful things like:
--3-53453---34-4534---
(of course, you should use \d instead of [0-9] if your regex engine has support for that).
You could try different ones:
\d* matches a string consisting only of digits
\d*-\d* matches a string of format digits - dash - digits
[0-9\-]* matches a string consisting of only dashes and digits
You can combine them via | (or), so that you have for example (\d*)|(\d*-\d*): matches formats just digits and digits-dash-digits.