I have a regex to validate the amount entered in a field: /^\d+(\.\d+)?$/
Can I somehow compound the expression such that I can check for the total no.of characters entered? It should allow for a max of 13 characters (with/without decimal)
Sure, just add a lookahead assertion at the start:
/^(?=.{0,13}$)\d+(\.\d+)?$/
^(?=.{0,13}$) makes sure that there are between 0 and 13 characters between start and end of string. It doesn't actually match and consume any of those characters, so the following part of the regex can then do the validation.
Another way would be
/^(?!.{14})\d+(\.\d+)?$/
Here, (?!.{14}) asserts that it's impossible to match 14 characters at the start of the string, thereby ensuring a length maximum of 13.
Other variations on this theme:
/^(?=.{13})\d+(\.\d+)?$/ # more than 12 characters
/^(?=.{6}$|.{8}$)\d+(\.\d+)?$/ # 6 or 8 characters
Lookahead :
/^(?=.{0,13}$)\d+(\.\d+)?$/
To define a maximum length you can use a positive lookahead
/^(?=.{0,13}$)\d+(\.\d+)?$/
(?=.{0,13}$) is a look ahead assertion, meaning are there between 0 and 13 characters ahead till the end of the string?
You can also do this separately for the part before and after the dot like this
^(?=[^.]{1,5}(?:\.|$))\d+(?:\.(?=.{1,4}$)\d+)?$
See it here online on Regexr
The first look ahead checks for NOT a dot ([^.]) 1 to 5 times, till it finds a dot or the end of the string. The second look ahead checks for 1 to 4 characters after the dot till the end of the string.
Related
The following regex working as expected other than the case that it's not allowed that all characters are the same characters.
^(?=[A-Z0-9]+[ -]?[A-Z0-9]+)(?!([A-Z0-9])(?:\1|[ -]){5,10}).{5,10}$
here minimum is 5 characters and the maximum is 10 characters
11114 allowed its minimum length matched as 5 and one charcter is diff so not all same charcters
11111115 allowed as one charcter is different and its more than 5 charcter.
2222222 not allowed as all are same characters
222-22 not allowed as all are same charcters
111-3 allowed as length 5 and one character is different
444-45 allowed as length more than 5
1234565436 allowed as length with in range 5 to 10
There is no need to repeat range quantifier {5,10} multiple times as that makes changing this regex harder for other cases.
You may use this regex for this:
^(?=.{5,10}$)([A-Z0-9])(?!(?:[ -]?\1)+$)[A-Z0-9]*[ -]?[A-Z0-9]+$
RegEx Demo
RegEx Breakup:
^: Start
(?=.{5,10}$): Assert that we have 5 to 10 chars till end
([A-Z0-9]): Match a letter or digit and capture in group #1
(?!(?:[ -]?\1)+$): Negative lookahead to fail the match if same captured value is repeated till end
[A-Z0-9]*: Match 0 or more letter or digit
[ -]?: Match optional space or hyphen
[A-Z0-9]+: Match 1 or more letter or digit
$: End
String = '11111111111110000000000000000000110000000000000011111111111111111111111111111111110011111111111110000011110000011111111111110000000000011111111111111111010001111111111111111111110011111111111111111111111111110111112111121111111111111111111000011000001011111111111101022111101111001111111111110000001000000111111111111111000000000000011111111111111100011111111001011111111100000000000000000000000000000000100111001000000000000000000011000000000000001111111000000000000000000000000000000000001111100000000000000000000011000000000000000000000010000000000333333333'
I want a pattern to take out 10 characters after the first 100 so i want to have 100 - 110 then I want to compare that one and see if that string with a length of 10 have 4 zeros in a row.
How can I do this with only Regex? I have been using substring before.
You could use this:
^.{100}(?=.{0,6}0000)(.{10})
Explanation:
^: matches the start of the string to avoid that the pattern is used anywhere in the input
.{100}: match 100 characters
(?= ): look ahead. This does not capture, but just verifies something that is still ahead.
.{0,6}: 0 to 6 characters
0000: literally 4 zeroes
(.{10}): 10 characters, this time they are captured and can be referenced back with \1 or $1 depending on the flavour of regex.
The above answer is perfect. But that matches all the characters including first 100.
In case of ignoring first 100, we can use
(?<=.{100})
To check the required pattern in last 10 characters after first 100 only, we can use
(?<=.{100})(?=.{0,6}0000)(.{10})
You can test it here
Update : I checked the link today. It's taking somewhere else.
I want to match 5 to 20 character with regex.
I try to use below regular expression for my checking.
/^[a-zA-Z][\w]{5,20}$/
It's work, but the problem of length it match 6 to 21 character.
(^[a-zA-Z][\w]){4,20}$
I also try this but it don't work.
Please anyone help me to match exact length of regex.
It's because your capturing group is expecting TWO characters:
[a-zA-Z] and [\w], that's two letters.
So your first attempt actually did this:
match [a-zA-Z] once
match [\w] once
match the previous matches 5 - 20 times
Inevitably, you always had 1 more match than expected
Capture only one character, and iterate it 5-20 times.
Have you tried:
^([a-zA-Z]{5,20})$ ?
OR
^(\w{5,20})$ ?
You're almost there, you just need to make a single range of characters (in square brackets) not two.
/^[a-zA-Z][\w]{5,20}$/ means:
a character from a to z in lower or upper case
5 to 20 word characters
That sums up to 6 to 21 characters in total.
I suppose you want /^[a-zA-Z][\w]{4,19}$/:
a character from a to z in lower or upper case
4 to 19 word characters
That sums up to 5 to 20 characters in total.
The Quantifier is only applied to the [\w]. So this expects exactly one letter character and then 5-20 whitespace characters.
I assume you want 5-20 characters that can be either a letter a-z or a whitespace. You need to group these together in square brackets and then apply the quantifier:
^[a-zA-Z\W]{5,20}$
So, I understand, you want a string that has 5-20 characters, starts with a letter and then only has letters and digits. You would write it like that:
^[a-zA-Z][a-zA-Z0-9]{4,19}$
This expects first a letter and then 4-19 letters or digits.
BTW: https://regex101.com/ is a great site to test regular expressions and get an explanation what they are doing.
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'm trying to make a javascript regex to match:
required: 9 or more numbers.
optional: dash, forward slash, plus and space characters.
forbidden: any other character
So far I have
^[0-9-+\/\s]{9,}$
The only problem with this (I think) is that it counts the non numeric permitted characters along to reach the minimum 9.
How can I amend it so that it only counts the numbers to reach the minimum 9?
If you want to solve this in a single RE (not necessarily recommended, but sometimes useful):
^[-+\/\s]*([0-9][-+\/\s]*){9,}$
Or, if you want the first and last characters to be digits:
^[0-9](^[-+\/\s]*[0-9]){8,}$
That's: a digit, followed by eight or more runs of the optional characters, each ending with a digit.
You can use lookahead to check if there are 9 or more digits anywhere
^(?=(\D*\d){9,})[\d/+ -]+$
--------------
|
|->match further only if there are 9 or more digits anywhere
OR
^([/+ -]*\d){9,}[/+ -]*$
^([0-9][-+\/\s]*){9,}$ should do.
its simple with look-ahead
Try this pattern:
^(?=([^\d]*\d){9,})[0-9-+\/\s]*$