Perl regex to match phone numbers [duplicate] - regex

This question already has answers here:
How to validate phone numbers using regex
(43 answers)
Closed 7 years ago.
Regex:
\b(\(\d{3}\)|\d{3})?[-.]?\d{3}[-]?\d{4}\b
My input file has two types of phone numbers. One, whose first 3 digits are enclosed in parenthesis and the other with no parenthesis.
Eg:
"(201)-450-4479" ,"234-345-3456"
I want to match both type of phone numbers using alternate operator.
Please suggest me. What modification is required for above mentioned expression to get the intended result?

\b matches at a word-nonword boundary. If such a boundary should appear before (, it must be preceded by a word character, not whitespace or nothing.
Cf.
print /\b\(/ ? 1 : 0 for '(', ' (', 'a(';
Remove the starting \b from the regex, or replace it with
(?x: \b | \s | ^ )

I'd use this:
(\(?\d+\)?\-\d+\-\d+)
or using the alternate operator:
(\d+\-\d+\-\d+|\(\d+\)\-\d+\-\d+)

Related

Need Regex to validate 11-digit phone number without plus sign [duplicate]

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}$

RegEx to match any character at the 3rd position [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I'm trying to match (extract) any character/symbol at the 3rd position of a string using regex. And no, I can't use substrings for this scenario. Below are examples I want matched:
ABCDEF => C
123456 => 3
A B C => B
I'm also being guaranteed to have a string of more than 10 characters so I don't have to worry about being less that 3 characters.
Any help would be appreciated.
You may use this regex with a negative lookbehind:
(?<=^..).
RegEx Demo
RegEx Details:
(?<=^..): Lookbehind assertion to match any 2 characters at line start
.: Match character at 3rd position

Regular Expression.how to add optional character at end of regex [duplicate]

This question already has answers here:
Regex how to match an optional character
(5 answers)
Closed 6 years ago.
I just want to write a regular expression 4 digits and '.' and 5 digits and optional 'A'
Ex: 1111.2345A where A is optional.
^[0-9]{4}[\.][0-9]{4}$
This reg ex will give 1111.2345, but how to add Optional 'N' at last.
Use ? at the end for characters:
[A-Za-z]?
This will match at most 1 presence of a character (lower or upper case).
You can check for a character zero or one times with this:
'[A]{0,1}'
Put that at the end of your string and it will try and match the character 'A' zero or one times. You may also use the symbol ? to match zero or one times. All about preference.
To get a single, optional A at the end, append A? to your regular expression:
^[0-9]{4}[\.][0-9]{4}A?$
Btw. instead of [0-9] you could use \d which stands for 'digit':
^\d{4}\.\d{4}A?$

Regex to validate letters numbers and specific special characters [duplicate]

This question already has answers here:
Regex for validating a string with pre-specified special characters
(3 answers)
Closed 8 years ago.
I have unsuccessfully be looking around the web for such a simple regex and can't seem to put it together.
I need a regex which allows any letter, numbers, whitespace and particular special characters only (# # $ & ( ) - _ /)
"Test #123 #Sample/test" is valid
"Test ^ £300" is not valid
Simply you could try the below regex,
^[\w\s##$&()\/-]+$
DEMO
^ Asserts that we are at the start.
[\w\s##$&()\/-]+ Matches one or more characters from the list.
$ Asserts that we are at the end.

Regexp pattern Optional character [duplicate]

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.