I need a regex for the following input:
[2 digits], comma, [two digits], comma, [two digits]
The 2 digits cannot start with 0. It is allowed to only enter the first 2 digits. Or to enter the first 2 digits, then a comma en then the next 2 digits. Or to enter the full string as described above.
Valid input would be:
10
99
17,56
15,99
10,57,61
32,44,99
Could anyone please help me with this regex?
At the moment I have this regex, but it doesn't limit the input to maximum 3 groups of 2 digits:
^\d{2}(?:[,]\d{2})*$
^[1-9]\d(?:,[1-9]\d){0,2}$
The first part ([1-9]\d) is simply the first number, which has to be present at all times. It consists of a non-zero digit and an arbitrary second digit (\d).
What follows is a non-capturing group ((?:...)), containing a comma followed by another two-digit number (,[1-9]\d), just alike the first one. This group can be repeated between zero and two times ({0,2}), so you get either no, one or two sequences of a comma and another number.
You can easily expand the part in the curly braces to allow for more allowed numbers.
^[1-9]\d([,][1-9]\d){0,2}$
Related
I have a large list of zip codes. Most of the zip codes are 5 digits but some are 9 digits.
I need to insert a hyphen after the 5th number but only if there are more than 5 numbers.
I can find those with 9 digits with
(^\d{9})
but I am unsure on how to replace.
With capturing groups and substitution we can achieve this.
Find what: (\d{5})(\d{4})
Replace with: $1-$2
This finds 5 digits followed by 4 digits and creates two capturing groups (one chunk with the first 5 digits and another chunk with the following 4). The '$'-sign followed by a number is a substitution. Here we are saying: Paste the first capturing group, insert a hyphen and paste the second capturing group.
Example: https://regex101.com/r/KnzTus/1
I need to be able to check if a string contains either a 2 digit or a 4 digit number before a . (period).
For example, 39. is good, and so is 3926., but 392. is not.
I originally had (^\\d{2,4).$) but that allows between a 2 and a 4 digit number preceding a period.
I also tried (^\\d{2}.|\\d{4}.$) but that didn't work.
You can use this regex:
^\d{2}(?:\d{2})?\.$
This regex makes 2nd set of \d{2} optional thus allowing to match 12. or 1234. but not 123..
In the expression (^\d{2}.|\d{4}.$), the dots match any character.
Try escaping them to make them match literal dots: (^\d{2}\.|\d{4}\.$)
I am trying to write a regex to max a sequence of numbers that is 5 digits long or over, but I ignore any spaces, dashes, parens, or hashes when doing that analysis. Here's what I have so far.
(\d|\(|\)|\s|#|-){5,}
The problem with this is that this will match any sequence of 5 characters including those characters I want to ignore, so something like "#123 " would match. While I do want to ignore the # and space character, I still need the number itself to be 5 digits or more in order to qualify at a match.
To be clear, these would match:
1-2-3-4-5
123 45
2(134) 5
Bonus points if the matching begins and ends with a number rather than with one of those "special characters" I am excluding.
Any tips for doing this kind of matching?
If I understood requirements right you can use:
^\d(?:[()\s#-]*\d){4,}$
RegEx Demo
It always matches a digit at start. Then it is followed by 4 or more of a non-capturing group i.e. (?:[()\s#-]*\d) which means 0 or more of any listed special character followed by a digit.
So just repeat a digit, followed by any other sequence of allowed characters 5 or more times:
^(\d[()\s#-]*){5,}$
You can ensure it ends on a digit if you subtract one of the repetitions and add an explicit digit at the end:
^(\d[()\s#-]*){4,}\d$
You can suggest non-digits with \D so et would be something like:
(\d\D*){5,}
Here is a guide.
I use the following in my pattern(logback.xml) to replace 10 digit numbers in my log.
%replace(%msg){'\d{10}','**********'}
One problem with this approach is, it also matches first 10 digits of 11 digit number.
Is there a way to match exactly 10 digits numbers.
Now the bigger problem is somehow I need to display the last two digits of this 10 digit number.
Use this:
%replace(%msg){'\b\d{10}\b','**********'}
\b is a word boundary that matches a position where one side is a letter, and the other side is not a letter (for instance a space character, or the beginning of the string)
To display (leave uncaptured) the last two digits, please see the following regex:
'\b\d{8}(?=\d{2}\b)'
View a regex demo!
This will find 8 numerical digits before two digits where the 10 digits are wrapped within word boundaries. Since (?= ) is positive lookahead assertion, it won't be matched. The entire match can then be replaced with:
********
No capturing groups necessary.
To get the last two digits of the corressponding 10 digit number,
'\b\d{8}(\d{2})\b'
First captured group contains the last two digits.
DEMO
Yes, if you use this (as #zx81 said):
\b\d{8}(\d\d)\b
(Explenation: http://www.regexper.com/#%5Cb%5Cd%7B8%7D(%5Cd%5Cd)%5Cb)
That will find 10 digits and store the last 2 digits in a group. If you replace that with a string like this:
********$1
That will replace the first 8 numbers, and leave the last two visible.
Example: http://regexr.com/3989s
I want to use phone number it should have 15 numeric digit and only two hyphen.
I am using this but it is not working properly:
[\d\-]{7,15}
A phone number (that contains two hyphens) consists of three groups of numbers. Let's assume 3 groups of 5 numbers like so.
xxxxx-xxxxx-xxxxx
The natural regex for this is
^\d{5}-\d{5}-\d{5}$
To change the groups change the numbers is curly brackets.
Try this:
^(?=(.*?-){,2})[\d-]{7,15}$
This allows input 7-15 digits/dashes, but at most 2 dashes
Note that you don't have to escape a dash when it's first it last within a character class.