need a regular expression to match tel number - regex

I want to match expression like (010)12345678 or 010-12345678. Now my regular expression is \(?\d{3}[)-]\d{8}, but it where match expression like (010-12345678. How should I write a true regular expression?
EDIT:
I write a right regex like (\(\d{3}\)|[^(]\d{3}-)\d{8} that can meet my needs.

Note that (\(\d{3}\)|[^(]\d{3}-)\d{8} will also match a non-( before a number that starts with 3 digits and - due to [^(].
You need to remove it and use
(\(\d{3}\)|\d{3}-)\d{8}
^^^^^^^^^^^^^^^^^^
See demo. Or an anchored version:
^(\(\d{3}\)|\d{3}-)\d{8}$
See another demo
Pattern details:
^ - start of string
(\(\d{3}\)|\d{3}-) - Group 1 matching either of the 2 alternatives:
\(\d{3}\) - ( followed with 3 digits and a literal )
| - or
\d{3}- - 3 digits and a literal -
\d{8} - 8 digits
$ - end of string.
Depending on the regex engine, \d might need replacing with [0-9].

Related

Regular Expression to Validate Monaco Number Plates

I would like to have an expression to validate the plates of monaco.
They are written as follows:
A123
123A
1234
I started by doing:
^[a-zA-Z0-9]{1}?[0-9]{2}?[a-zA-Z0-9]{1}$
But the case A12A which is false is possible with that.
You can use
^(?!(?:\d*[a-zA-Z]){2})[a-zA-Z\d]{4}$
See the regex demo. Details:
^ - start of string
(?!(?:\d*[a-zA-Z]){2}) - a negative lookahead that fails the match if there are two occurrences of any zero or more digits followed with two ASCII letters immediately to the right of the current location
[a-zA-Z\d]{4} - four alphanumeric chars
$ - end of string.
You can write the pattern using 3 alternatives specifying all the allowed variations for the example data:
^(?:[a-zA-Z][0-9]{3}|[0-9]{3}[a-zA-Z]|[0-9]{4})$
See a regex demo.
Note that you can omit {1} and
To not match 2 chars A-Z you can write the alternation as:
^(?:[a-zA-Z]\d{3}|\d{3}[a-zA-Z\d]|\d[a-zA-Z\d][a-zA-Z\d]\d)$
See another regex demo.
So it needs 3 connected digits and 1 letter or digit.
Then you can use this pattern :
^(?=.?[0-9]{3})[A-Za-z0-9]{4}$
The lookahead (?=.?[0-9]{3}) asserts the 3 connected digits.
Test on Regex101 here

Regex with wildcard search?

I created a Regex to check a string for the following situation:
first 4 chars are numbers
following by a point
following by 3 numbers
following by a point
following by 4 to 8 numbers or letters
ie: 1234.123.125B
My Regex: ^[0-9]{4}[.][0-9]{3}[.][0-9a-zA-Z]{4,8}$
But now I need a wildcard search: The Regex should also match if there is a '*' after the first 8 characters. For example:
1234.123.12* MATCH
1234.123* MATCH
1234.123.45B9* MATCH
1234.12* NO MATCH
1234.12345* NO MATCH
How can I add the wildcard search to my Regex?
Thank you
You may use this regex with alternation:
^\d{4}\.\d{3}(?:\*|\.[\da-zA-Z]{0,7}\*|\.[\da-zA-Z]{4,8})$
RegEx Demo
RegEx Details:
^: Start
\d{4}\.\d{3}: Match 4 digits + 1 dot + 3 digits
(?:\*|\.[\da-zA-Z]{0,7}\*|\.[\da-zA-Z]{4,8}): matches a single * OR a * after after a dot and 0 to 7 digits/letters OR match 4 to 8 digits/letters
$: End
My assumptions are that:
You don't allow wildcards to be mid-string
Nor do you want to allow wildcards after the full pattern (e.g.: 1234.123.12345678*).
So, alternatively you may possibily use something like:
^\d{4}\.\d{3}(?!.*\*.)(?![^*]{0,4}$)[.*][*\da-zA-Z]{0,8}$
See the online demo.
^ - Start string ancor.
\d{4}\.\d{3} - Four digits, a dot and another three digits.
(?!.*\*.) - Negative lookahead for zero or more characters followed by asterisk and another character other than newline.
(?![^*]{0,4}$) - Negative lookahead for zero to four characters other than asterisk before end string ancor.
[.*] - A literal dot or asterisk.
[*\da-zA-Z]{0,8} - Zero to eight characters from the character class.
$ - End string ancor.

What expression should I use to get desired results?

For strings like Cisco 3750 i7706-cm021 10.123.12.34 -> 10.123.34.12 I would like to get result Cisco 3750 i7706-cm021 10.123.12.34 -> using expression ^.*(?![\d\.]{12}$). But instead a whole string is matched. What is the correct expression would be?
You may use a regex like
^.*?(?=\b(?:\d{1,3}\.){3}\d{1,3}$)
See the regex demo and the Regulex graph:
Details
^ - start of string
.*? - any 0+ chars other than line break chars, as few as possible
(?=\b(?:\d{1,3}\.){3}\d{1,3}$) - a positive lookahead that requires (immediately to the right of the current location):
\b - word boundary
(?:\d{1,3}\.){3} - three repetitions of 1 to 3 digits and a dot
\d{1,3} - one to three digits
$ - end of string.
To get more precise IP regex, see How to Find or Validate an IP Address:
^.*?(?=\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$)
See the regex demo

Regex for Chilean RUT/RUN with PCRE

I'm having issues with the validation of the chilean RUT/RUN with a regex expression in PCRE. I have the next regular expression but sadly can't make it work:
\b[0-9|.]{1,10}\-[K|k|0-9]
I need help to see what is wrong with the code. The application I need to use only uses PCRE.
Thank you.
You may use
^(\d{1,3}(?:\.\d{1,3}){2}-[\dkK])$
to match and capture (that is not usually necessary, but your app requires a capturing group to extract its contents) a whole string that matches the pattern. See the regex demo.
To match shorter strings that match this pattern inside a larger string, you may remove ^ and $ (see demo) or use \b word boundaries instead (see this demo).
Details:
^ - start of string
\d{1,3} - 1 to 3 digits
(?:\.\d{1,3}){2} - 2 sequences of a literal . and 1 to 3 digits
- - a hyphen
[\dkK] - a digit, k or K.
$ - end of string.
As they sometimes omit the dots, I used this one:
^(\d{1,2}(?:[\.]?\d{3}){2}-[\dkK])$
Details:
^ - start of string
\d{1,2} - 1 or 2 digits
(?:[.]?\d{3}){2} - 2 sequences of an optional '.' and 3 digits
- a hyphen
[\dkK] - a digit, k or K
$ - end of string
1234567-k OK
12345678-k OK
1.234.567-k OK
12.345.678-k OK
known issue:
12.345678-k and 12345.678-k still OK and I do not like this :(
You need to change to ^(\d{1,3}(?:\.\d{3}){2}-[\dkK])$ to capture only 2 sequence of 3 digits after the first sequence of 1-3 digits.
please consider being more specific in the REGEX build, since it matched wrong numbers, such as 17.87.335-2. Also the included one did't match formats without the dots or the hyphens.
Please consider using the following format: \b(\d{1,3}(?:(.?)\d{3}){2}(-?)[\dkK])\b
Modified prior version to try the other formats: https://regex101.com/r/2Us0j6/9

Regex optional and depending on older optional

I currently have this regex:
/^\+\(?([0-9]{1,4})\)?$/g
Now, I can use the following formats:
+31
+(31)
+(31
+31)
However, I want to modify it so only with both parentheses it'll work, not just with one. So +(31 and +31) won't work anymore. How can I manage to get it?
Create an alternation group to match digits inside parentheses or with no parentheses:
^\+(?:\([0-9]{1,4}\)|[0-9]{1,4})$
See regex demo
Details:
^ - start of string anchor
\+ - a literal +
(?:\([0-9]{1,4}\)|[0-9]{1,4}) - one of the two alternatives:
\([0-9]{1,4}\) - 1 to 4 digits enclosed with literal ( and )
| - or
[0-9]{1,4} - 1 to 4 digits
$ - end of string anchor