so I got an assesment due and one the regular expressions is a phone digit that has 10 numbers but it also allows dashes "-". However it can only contain 2 dashes and start with 0. eg. 0x-xxxx-xxxx where x can be any digit between 0-9.
So, so far I've come up with the following regexes:
^[0-0][0-9-]{1,11}$
^[0-0][0-9-]{11}$
In the first one it works but allows any length, if I put 5 numbers it goes through as long as the first one is 0. In the second one I can put 2 dashes followed by each other or just fill it with dashes and it goes through.
Thanks for helping guys!
Match every of the three parts, separated by the dashes, like this:
^0\d-\d{4}-\d{4}$
This matches 0x-xxxx-xxxx. Demo: https://regex101.com/r/nW7wL5/1
If you also want to match the number without the dashes, use
^0\d-?\d{4}-?\d{4}$
Demo: https://regex101.com/r/gY0mC3/1
\d is the same as [0-9] but it's shorter.
Related
I've written a regular expression that matches any number of letters with any number of single spaces between the letters. I would like that regular expression to also enforce a minimum and maximum number of characters, but I'm not sure how to do that (or if it's possible).
My regular expression is:
[A-Za-z](\s?[A-Za-z])+
I realized it was only matching two sets of letters surrounding a single space, so I modified it slightly to fix that. The original question is still the same though.
Is there a way to enforce a minimum of three characters and a maximum of 30?
Yes
Just like + means one or more you can use {3,30} to match between 3 and 30
For example [a-z]{3,30} matches between 3 and 30 lowercase alphabet letters
From the documentation of the Pattern class
X{n,m} X, at least n but not more than m times
In your case, matching 3-30 letters followed by spaces could be accomplished with:
([a-zA-Z]\s){3,30}
If you require trailing whitespace, if you don't you can use: (2-29 times letter+space, then letter)
([a-zA-Z]\s){2,29}[a-zA-Z]
If you'd like whitespaces to count as characters you need to divide that number by 2 to get
([a-zA-Z]\s){1,14}[a-zA-Z]
You can add \s? to that last one if the trailing whitespace is optional. These were all tested on RegexPlanet
If you'd like the entire string altogether to be between 3 and 30 characters you can use lookaheads adding (?=^.{3,30}$) at the beginning of the RegExp and removing the other size limitations
All that said, in all honestly I'd probably just test the String's .length property. It's more readable.
This is what you are looking for
^[a-zA-Z](\s?[a-zA-Z]){2,29}$
^ is the start of string
$ is the end of string
(\s?[a-zA-Z]){2,29} would match (\s?[a-zA-Z]) 2 to 29 times..
Actually Benjamin's answer will lead to the complete solution to the OP's question.
Using lookaheads it is possible to restrict the total number of characters AND restrict the match to a set combination of letters and (optional) single spaces.
The regex that solves the entire problem would become
(?=^.{3,30}$)^([A-Za-z][\s]?)+$
This will match AAA, A A and also fail to match AA A since there are two consecutive spaces.
I tested this at http://regexpal.com/ and it does the trick.
You should use
[a-zA-Z ]{20}
[For allowed characters]{for limiting of the number of characters}
I'm having a bit of a time figuring this out. I was able to put together regex to allow negative numbers for money and decimals in a numeric field. However, the problem I'm having is that someone can put just a dash "-" and it does not bomb out. How do I allow for the dash, but it can't only be the dash?
Here is what I have. Tried adding something along the lines of [^-]? but it doesn't do what I need.
(-)?(\$)?([\d]{1,})?(\.([\d]{1,})?)?
This regular expression might work for your case:
^-?\$?\d+(?:\.\d+)?
Starts with zero or one hyphens, followed by zero or one dollar signs then one or more digits, followed by zero or one groups of a dot and one or more digits.
Regardless of whether the hyphen, dollar sign, decimal part, or all are present, there needs to be one or more digits
It's been a while that I am juggling around this. Hope you can give me
some pointers.
All I want to achieve is, the string should contain EXACTLY 4 '-' and 10 digits in any giver order.
I created this regex : ^(-\d-){10}$
It does enforce max-length of 10 on digits but I am not getting a way to implement max-length of 4 for '-'
Thanks
Ok, here's a pattern:
^(?=(?:\d*?-){4}\d*$)(?=(?:-*?\d){10}-*$).{14}$
Demo
Explanation:
The main part is ^.{14}$ which simply checks there are 14 characters in the string.
Then, there are two lookaheads at the start:
(?=(?:\d*?-){4}\d*$)
(?=(?:-*?\d){10}-*$)
The first one checks the hyphens, and the second one checks the digits and make sure the count is correct. Both match the entire input string and are very similar so let's just take a look at the first one.
(?:\d*?-){4} matches any number of digits (or none) followed by a hyphen, four times. After this match, we know there are four hyphens. (I used an ungreedy quantifier (*?) just to prevent useless backtracking, as an optimization)
\d*$ just makes sure the rest of the string is only made of digits.
I have an input box and the condition is to allow the user to enter only numbers, the numbers entered should be in the following format in groups of 4, ex: 4444 5555 and the maximum number of characters to be entered in the textbox should be 9. I am pretty new to regex, so have no clue of how to start. A working sample in fiddle would be of great help.
If the requirement is strictly 10 numbers in the above grouping with spaces in the middle, the regex is simple:
/^\d{4}\s\d{4}\s\d{2}$/
Where \d means that it would only match a numeric character, {4} means that it would look exactly 4 times for the previous match (\d), and in this case that would match 4 numeric characters. \s means one whitespace, and similarly like the {4}, \d{2} matches 2 numeric characters. The ^ and $ mean start of the string to be matched and end of the string to be matched respectively.
Hope this helps.
If the length is fixed then you can just use \d to represent a digit
/^\d\d\d\d \d\d\d\d \d\d\d\d \d\d$/
or use the {n} multiplier instead
/^\d{4} \d{4} \d{4} \d\d$/
if instead the total length is arbitrary and you just want to be sure that every four digits you have a space things are just slightly more complex:
/^(\d{4} )*\d{1,4}$/
the meaning is that you want zero or more groups formed with 4 digits and one space followed by 1 to 4 digits. In the last part you can use {0,4} if you also want to accept an empty string as a valid response.
If you want 1 or more of something use '+'. For example 4+ would be 1 or more consecutive '4's.
Use * to for things that you want 0 or more of!
Use parentheses for groups of characters or groups of other groups.
If you want a space in between, then use the space character between two of them.
It looks like you want 1 or more '4's followed by 0 or more (space followed by 1 or more '4's)
This regular express would match all of the following strings: "4+( 4+)*"
44444
4 44 4
4 4 4
4
4444444444
4 4
44444444444444 44444444444444444 4444444444444444
4444 4444 44
As per example provided this regex will help
/^[0-9][0-9 ]*$/
This represent numbers with spaces in between. For eg. 444 444. But if you put in this way ' 444 444' like first inserting space then start the numbers then it wont allow.
For that you can use /^[0-9 ]*$/
^ represent start and $ represent end. So between start and end you can write numbers with spaces.
Could you please help me to provide a RegEx pattern to validate a string which satisfy:
length from 4 to 10 (strictly)
first 3 characters must be string (A-Z a-z)
the remain characters must be number without 00 as prefix, I mean ABC15 is passed but ABC0015 is not.
This problem took me so much time and I have tried so many regex patterns, but I still have no solution for it.
Thank you so much.
Use this one:
/^[a-z]{3}(?!00)\d{1,7}$/i
Explanation:
/
^ Start
[a-z]{3} Three letters.
(?!00) Must NOT be followed by two zeros.
\d{1,7} One to seven digits.
$ End.
/i ignore case flag.
Easy.
/^[a-z]{3}[1-9][0-9]{0,6}$/i
Matches three letters (case insensitive flag at the end), followed by one digit that is not zero, followed by up to six more digits (which may be zero).