Regular expression for parsing a phone number [duplicate] - regex

This question already has answers here:
How to validate phone numbers using regex
(43 answers)
Closed 7 years ago.
I need a regular expression for a phone number.
Example,
1234567899 should be (123)(456)(7899)
12345678 should be (123)(456)(78)
1234567 should be (123)(456)(7)
123456 should be (123)(456)
12345 should be (123)(45)
123 should be (123)
1 should be (1)
I tried /([0-9]{0,3})([0-9]{0,3})([0-9]{0,4})/ and /([0-9]{3})([0-9]{3})([0-9]{4})/
But it takes only when all the 10 numbers are in the input.
I need match then replace with (

This would appear to work:
/(\d{1,3})(\d{1,3})?(\d{1,4})?/
Live test here

Check this : /^\(\d{0,3}\)(\(\d{0,3}\))?(\(\d{0,4}\))?$/
https://regex101.com/r/qC0fS9/2

use this pattern
((^\d{1,3})|(?<=^\d{3})(\d{1,3})|(\d+$))
or simplified to
(^\d{3}|(?<=^\d{3})\d{3}|\d+$)
and replace with (\1)
Demo

Related

how to validate first three digits of phone number [duplicate]

This question already has answers here:
How to validate phone numbers using regex
(43 answers)
Closed 2 years ago.
I have a TextFormField for a phone number and I want to validate the input to match my RegEx to phone number value
the valid inputs are
075xxxxxxxx
077xxxxxxxx
078xxxxxxxx
079xxxxxxxx
I tried this r"^ (?:[0]7)?[0-9]{11}$" but its not working
any way to do it right?
Match a zero, then a 7, then any of [5,7,8,9] then any 8 digits:
07[5789]\d{8}

Multi-part Regular Expression [duplicate]

This question already has answers here:
Comma Separated Numbers Regex
(6 answers)
Closed 3 years ago.
I have the requirement to restrict a non-required textbox to only numbers (0-9) with a separator of ';'. The pattern is that the groups can be 4 or 5 in length and can repeat n times. Anything less than 4 in a group is invalid. After 4 or 5 I need to check for the existence of a separator character ';'. This pattern can repeat n times. I have tried variations of but this doesn't seem to be working. Something simple to start out like
[0-9]{4,5};+
is invalid as I don't need the separator for only 1 number grouping.
Next I tried
^[0-9]{4,5}|[0-9]{4,4};|[0-9]{5,5};$
but this doesn't work because the existence of four digits like 1111 or five digits 11111 before gives one match before it goes awry example "11111;j" Is there a way in a regex to validate
1111
11111
1111;1111
11111;1111
11111;11111
but catch
111
111;
1111;1
11111;1
abc
in a repeating fashion?
This validate your examples.
^[0-9]{4,5}(;[0-9]{4,5})?$
Try it
It's not clare what you mean by "in a repeating fashion". If you want validate also this
1111;11111;11111;1111;11111
You can use this regex
^[0-9]{4,5}(;[0-9]{4,5})*$
Try it

Phone number regex should strictly allow 10 characters if starting with 02/03/04/07/08 [duplicate]

This question already has answers here:
Regex for Australian phone number validation
(5 answers)
Closed 4 years ago.
The below expression is being used to accept Australian phone numbers.
I need to change the expression as to strictly accept total 10 digits (without spaces) if the number starts with 02/03/04/07/08.
^\({0,1}((0|\+61)\s?(2|4|3|7|8)){0,1}\){0,1}(\ |-){0,1}[0-9]{2}(\ |-){0,1}[0-9]{2}(\ |-){0,1}[0-9]{1}(\ |-){0,1}[0-9]{3}$
It does accept 10 digits if the number is entered like 03 11 11 1 111, but without spaces 8 digit number is accepted too.
You can use the use the following regex with alternation:
^ *(?:0 *[23478](?: *\d){8}|[1-9](?: *\d)*|0 *[01569](?: *\d)*) *$
Demo: https://regex101.com/r/bet7m1/1

Golang split strings by char type [duplicate]

This question already has answers here:
Regex split numbers and letter groups without spaces
(2 answers)
Closed 5 years ago.
I want to be able to split the string into substrings where characters and numbers are separate groups:
re:=regexp.MustCompile("**MAGIC HERE**")
fmt.Println(re.FindAllString("abc123def", -1))
I want to be able to get
[abc 123 def]
Any ideas?
Try splitting on this pattern:
\d+|\D+
Code:
re:=regexp.MustCompile("\\d+|\\D+")
fmt.Println(re.FindAllString("abc123def", -1))
Output:
[abc 123 def]
Demo here:
Rextester

regex having a alphabet in a string [duplicate]

This question already has answers here:
Regex to match only letters
(20 answers)
Closed 9 years ago.
I was looking for a regex that can validate phone numbers, but somehow i cannot find a universal solution.
So i just want to check if the string has any alphabets that is a-z
If it does not then pass it, for example
000 -> Pass
000(1) -> Pass
000a -> Fail
(?mx)^(?=.*?([0-9]))((?![a-zA-Z]).)*$
This will check to see if your line has any numbers in it while NOT having any alpha chars. See the example here.
its for phone numbers validation
RegExp(/^[0-9 +()-]{3,30}$/i)
for only string
RegExp(/^[a-zA-Z]{1,2}$/i)