Regex patten cutom implementation - regex

10001.000.01.01-A-AB - I need to write regex in the following format. this is taking care of until numbers decimal need to add characters
/^\d{4,6}(\.\d{3})(\.\d{2}(\.\d{2})?(\.\d{2})?)?$/
0001.000-A
0001.000.01-A
0001.000.01.01-A
0001.000.01.01-A-AB
10001.000.01.01-A-AB
Any help greatly appreciated.

It seems you may use
^\d{4,6}\.\d{3}(?:\.\d{2}(?:\.\d{2})?(?:\.\d{2})?)?(?:-[A-Z]+(?:-[A-Z]+)?)?$
See the regex demo
Details
^ - start of string
\d{4,6} - 4 to 6 digits
\.\d{3} - a . and 3 digits
(?:\.\d{2}(?:\.\d{2})?(?:\.\d{2})?)? - an optional group matching
\.\d{2} - a dot and 2 digits
(?:\.\d{2})? - an optional sequence of . and 2 digits
(?:\.\d{2})? - ibid.
(?:-[A-Z]+(?:-[A-Z]+)?)? - an optional non-capturing group matching 1 or 0 occurrences of:
- - a hyphen
[A-Z]+ - 1 or more ASCII uppercase letters
(?:-[A-Z]+)? - an optional sequence of:
- - a hyphen
[A-Z]+ - 1 or more ASCII uppercase letters
$ - end of string

Related

Issue in removing a special character from regex to format the phone number

Working to format the phone number in .rdl reporting. I have below Regex in the expression property of the report field.
Possible inputs from user - 720) 352-6511 , +1 (469) 292-4242, 310.614.1316, (310) 468-0516
desired Output - (303) 233-2345
My regex =System.Text.RegularExpressions.Regex.Replace(IIf(IsNothing(Fields!Contact_Phone.Value), "", Fields!Contact_Phone.Value), "(\d{3})[ -.]*(\d{3})[ -.]*(\d{4})", "($1) $2-$3")
My output - ((303) 233-2345
There is an extra open parenthesis at the starting, the other options i tired are not working.
Thanks
Here is a very generic approach to fixing this issue: match any non-digit chars between the possible items in the input text and capture the three parts that look obligatory in the input:
^[^\d+]*(?:\+\D*\d+\D+)?(\d{3})\D*(\d{3})\D*(\d{4})\D*$
See the regex demo. Details:
^ - start of string
[^\d+]* - zero or more chars other than digits and +
(?:\+\D*\d+\D+)? - an optional sequence of +, zero or more non-digits, one or more digits, one or more non-digits
(\d{3}) - Group 1: three digits
\D* - zero or more non-digits
(\d{3}) - Group 2: three digits
\D* - zero or more non-digits
(\d{4}) - Group 3: four digits
\D* - zero or more non-digits
$ - end of string.

Regular Expression positive lookbehind

I would like to create a regex, that allowes the following patterns:
1234
1234567
123456789
12345678900-
12345678900-123456
It should be possible to only insert numbers and only one hyphen is allowed.
I tried with the following regex:
^[0-9]{1,11}(?(?<=\d{11})[-]?|)[0-9]{6}
It should not be possible to have 11 characters without the hyphen at the end(12345678900 is wrong).
Unfortunatly it didnt work as I intended.
You can match 1-10 digit and optionally match 1 digit followed by - and 6 digits.
^\d{1,10}(?:\d?-(?:\d{6})?)?$
^ Start of string
\d{1,10} Match 1-10 digits
(?: Non capture group
\d?- Match a single optional digit and -
(?:\d{6})? Match optional 6 digits
)? Close non capture group and make it optional
$ End of string
Regex demo
Another variation could be matching 1-10 digits or match 11 digits with a hyphen and optionally 6 digits if the hyphen should only possible after 11 digits.
^(?:\d{1,10}|\d{11}-(?:\d{6})?)$
Regex demo
You can use
^[0-9]{1,11}(?:(?<=[0-9]{11})-(?:[0-9]{6})?)?$
^\d{1,11}(?:(?<=\d{11})-(?:\d{6})?)?$
See the regex demo. Using \d is possible in case it matches ASCII digits in your regex flavor or you do not care if \d matches all possible Unicode digit chars or not.
Details:
^ - start of string
[0-9]{1,11} - one to eleven digits
(?:(?<=[0-9]{11})-(?:[0-9]{6})?)? - an optional occurrence of
(?<=[0-9]{11}) - immediately to the left there must be 11 digits
- - a hyphen
(?:[0-9]{6})? - an optional occurrence of six digits
$ - end of string.

What is the Regular Expression to allow only one hyphen(-) only between 2 letters or a letter and a number but not 2 numbers

I was using the below pattern.
/^[A-Za-z0-9]+(-[A-Za-z0-9]+)*$/.
What i need is that it should not be allowing the hyphen between 2 numbers.
I know that we have to make modification with 0-9, where we can restrict user from entering them twice.
The (?!.*[0-9]-[0-9]) lookahead after ^ will make sure there is no digit-digit pattern in the string. Also, if there must be 1 or 0 hyphens, replace * at the end with ? (0 or more occurrences).
Use
^(?!.*[0-9]-[0-9])[A-Za-z0-9]+(-[A-Za-z0-9]+)?$
See the regex demo.
Details
^ - start of string
(?!.*[0-9]-[0-9]) - a negative lookahead that fails the match if, after any 0+ chars other than line break chars, there is a digit, hyphen, digit pattern
[A-Za-z0-9]+ - 1 or more ASCII alphanumeric chars
(-[A-Za-z0-9]+)? - 1 or 0 sequences of:
- - a hyphen
[A-Za-z0-9]+ - 1 or more ASCII alphanumeric chars
$ - end of string.

RegEx that keeps the first 3 characters and deletes the rest

I have an identifier that contains letters or digits and dashes.
What I would like to do is to keep the first 3 letters before the first dash and delete the rest and then keep the 2 first letters after the first dash.
For instance, I have the following id :
9D3236A9-B496-4597-87E4-3A3FB69D07BF
The output ID should be : 9D3B445873A3.
I have tried:
^.{3}\-
but nothing happens. Can you please help with that?
You may use
^([A-Za-z0-9]{3})[A-Za-z0-9]*|-([A-Za-z0-9]{3})[A-Za-z0-9]*$|-([A-Za-z0-9]{2})[A-Za-z0-9]*
Replace with $1$2$3. See the regex demo.
Details
^ - start of string
([A-Za-z0-9]{3}) - Group 1 ($1 in the replacement): 3 alphanumeric chars
[A-Za-z0-9]* - 0+ alphanumerics
| - or
- - a hyphen
([A-Za-z0-9]{3}) - Group 2 ($2 in the replacement): 3 alphanumeric chars
[A-Za-z0-9]* - 0+ alphanumerics
$ - end of string
|
- - a hyphen
([A-Za-z0-9]{2}) - Group 3 ($3 in the replacement): 2 alphanumeric chars
[A-Za-z0-9]* - 0+ alphanumerics.
You can use the regex given in this demo
(^.{3})[a-z0-9A-Z]*((?>-).{2})[a-z0-9A-Z]*((?>-).{2})[a-z0-9A-Z]*((?>-).{2})[a-z0-9A-Z]*((?>-).{2})[a-zA-Z0-9]*

RegEx to match 2 or more digits in a string

Suppose I have strings like:
ABC-L-W7P-1423
ABC-L-W7E-87
CH-L-W7-756
I need to grab the number at the end. That number might be 2, 3 or 4 digits. But currently what I have is:
=REGEXREPLACE(B2,"[^0-9]","")
Which of course also grabs the '7' in 'W7P' which I don't want.
EDIT:
I also need to match something like this:
CH-M-311-MM
So always a 2, 3 or 4 (or 5) digit number, but I need single digits excluded.
You can use =REGEXEXTRACT with \b[0-9]{2,4}\b:
=REGEXEXTRACT(B2, "\b[0-9]{2,4}\b")
See the regex demo.
Details:
\b - a leading word boundary
[0-9]{2,4} - 2 to 4 digits
\b - trailing word boundary
In case your 2-4 digits are always preceded with -, you may use
=REGEXREPLACE(B2,"^.*-([0-9]{2,4})\b.*","$1")
See this regex demo
Details:
^ - start of string
.*- - any 0+ chars up to the last - that is followed with...
([0-9]{2,4}) - (Group 1 referred to with $1 in the replacement pattern) - 2 to 4 digits
\b - a trailing word boundary
.* - any chars up to the end of string.
I'm not sure which language you use, but if it supports lookarounds, you can assert that there is a - (dash) on the left side.
(?<=-)\d+
See: https://regex101.com/r/sI9zR9/1