I'm using regex and I want the number from 00001 to 99999. It should have 5 digits.
I know I can use [0-9]{5}, but then I have the number 00000, but it should begin at 00001.
(?!00000)[0-9]{5}
Prefixing (?!00000) ensures you don't match it; it's called negative lookahead.
Live demo here.
This rather ugly regexp requires that at least one of the 5 digits is not a zero. Therefore, the only 5 digit number that fails is 00000. But I think there must be a more elegant way.
^([1-9]\d{4}|\d[1-9]\d{3}|\d{2}[1-9]\d{2}|\d{3}[1-9]\d|\d{4}[1-9])$
(?!00000)[0-9]{5}
Explanation:
Negative Lookahead (?!00000) - Assert that the Regex does not match it
[0-9] a single character in the range between 0 and 9
{5} Matches exactly 5 times
Related
I need a regular expression that validates a number, but doesn't require a digit after the decimal.
ie.
123
123.
123.4
would all be valid
123..
would be invalid
Any would be greatly appreciated!
Use the following:
/^\d*\.?\d*$/
^ - Beginning of the line;
\d* - 0 or more digits;
\.? - An optional dot (escaped, because in regex, . is a special character);
\d* - 0 or more digits (the decimal part);
$ - End of the line.
This allows for .5 decimal rather than requiring the leading zero, such as 0.5
/\d+\.?\d*/
One or more digits (\d+), optional period (\.?), zero or more digits (\d*).
Depending on your usage or regex engine you may need to add start/end line anchors:
/^\d+\.?\d*$/
Debuggex Demo
You need a regular expression like the following to do it properly:
/^[+-]?((\d+(\.\d*)?)|(\.\d+))$/
The same expression with whitespace, using the extended modifier (as supported by Perl):
/^ [+-]? ( (\d+ (\.\d*)?) | (\.\d+) ) $/x
or with comments:
/^ # Beginning of string
[+-]? # Optional plus or minus character
( # Followed by either:
( # Start of first option
\d+ # One or more digits
(\.\d*)? # Optionally followed by: one decimal point and zero or more digits
) # End of first option
| # or
(\.\d+) # One decimal point followed by one or more digits
) # End of grouping of the OR options
$ # End of string (i.e. no extra characters remaining)
/x # Extended modifier (allows whitespace & comments in regular expression)
For example, it will match:
123
23.45
34.
.45
-123
-273.15
-42.
-.45
+516
+9.8
+2.
+.5
And will reject these non-numbers:
. (single decimal point)
-. (negative decimal point)
+. (plus decimal point)
(empty string)
The simpler solutions can incorrectly reject valid numbers or match these non-numbers.
this matches all requirements:
^\d+(\.\d+)?$
Try this regex:
\d+\.?\d*
\d+ digits before optional decimal
.? optional decimal(optional due to the ? quantifier)
\d* optional digits after decimal
I ended up using the following:
^\d*\.?\d+$
This makes the following invalid:
.
3.
This is what I did. It's more strict than any of the above (and more correct than some):
^0$|^[1-9]\d*$|^\.\d+$|^0\.\d*$|^[1-9]\d*\.\d*$
Strings that passes:
0
0.
1
123
123.
123.4
.0
.0123
.123
0.123
1.234
12.34
Strings that fails:
.
00000
01
.0.
..
00.123
02.134
you can use this:
^\d+(\.\d)?\d*$
matches:
11
11.1
0.2
does not match:
.2
2.
2.6.9
^[+-]?(([1-9][0-9]*)?[0-9](\.[0-9]*)?|\.[0-9]+)$
should reflect what people usually think of as a well formed decimal number.
The digits before the decimal point can be either a single digit, in which case it can be from 0 to 9, or more than one digits, in which case it cannot start with a 0.
If there are any digits present before the decimal sign, then the decimal and the digits following it are optional. Otherwise, a decimal has to be present followed by at least one digit. Note that multiple trailing 0's are allowed after the decimal point.
grep -E '^[+-]?(([1-9][0-9]*)?[0-9](\.[0-9]*)?|\.[0-9]+)$'
correctly matches the following:
9
0
10
10.
0.
0.0
0.100
0.10
0.01
10.0
10.10
.0
.1
.00
.100
.001
as well as their signed equivalents, whereas it rejects the following:
.
00
01
00.0
01.3
and their signed equivalents, as well as the empty string.
What language? In Perl style: ^\d+(\.\d*)?$
What you asked is already answered so this is just an additional info for those who want only 2 decimal digits if optional decimal point is entered:
^\d+(\.\d{2})?$
^ : start of the string
\d : a digit (equal to [0-9])
+ : one and unlimited times
Capturing Group (.\d{2})?
? : zero and one times
. : character .
\d : a digit (equal to [0-9])
{2} : exactly 2 times
$ : end of the string
1 : match
123 : match
123.00 : match
123. : no match
123.. : no match
123.0 : no match
123.000 : no match
123.00.00 : no match
try this. ^[0-9]\d{0,9}(\.\d{1,3})?%?$ it is tested and worked for me.
Regular expression:
^\d+((.)|(.\d{0,1})?)$
use \d+ instead of \d{0,1} if you want to allow more then one number use \d{0,2} instead of \d{0,1} if you want to allow up to two numbers after coma. See the example below for reference:
or
^\d+((.)|(.\d{0,2})?)$
or
^\d+((.)|(.\d+)?)$
Explanation
(These are generated by regex101)
^ asserts position at start of a line
\d matches a digit (equivalent to [0-9])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
1st Capturing Group ((.)|(.\d{0,1})?)
1st Alternative (.)
2nd Capturing Group (.)
. matches any character (except for line terminators)
2nd Alternative (.\d{0,1})?
3rd Capturing Group (.\d{0,1})?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
. matches any character (except for line terminators)
\d matches a digit (equivalent to [0-9])
{0,1} matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
$ asserts position at the end of a line
Sandbox
Play with regex here: https://regex101.com/
(?<![^d])\d+(?:\.\d+)?(?![^d])
clean and simple.
This uses Suffix and Prefix, RegEx features.
It directly returns true - false for IsMatch condition
^\d+(()|(\.\d+)?)$
Came up with this. Allows both integer and decimal, but forces a complete decimal (leading and trailing numbers) if you decide to enter a decimal.
In Perl, use Regexp::Common which will allow you to assemble a finely-tuned regular expression for your particular number format. If you are not using Perl, the generated regular expression can still typically be used by other languages.
Printing the result of generating the example regular expressions in Regexp::Common::Number:
$ perl -MRegexp::Common=number -E 'say $RE{num}{int}'
(?:(?:[-+]?)(?:[0123456789]+))
$ perl -MRegexp::Common=number -E 'say $RE{num}{real}'
(?:(?i)(?:[-+]?)(?:(?=[.]?[0123456789])(?:[0123456789]*)(?:(?:[.])(?:[0123456789]{0,}))?)(?:(?:[E])(?:(?:[-+]?)(?:[0123456789]+))|))
$ perl -MRegexp::Common=number -E 'say $RE{num}{real}{-base=>16}'
(?:(?i)(?:[-+]?)(?:(?=[.]?[0123456789ABCDEF])(?:[0123456789ABCDEF]*)(?:(?:[.])(?:[0123456789ABCDEF]{0,}))?)(?:(?:[G])(?:(?:[-+]?)(?:[0123456789ABCDEF]+))|))
For those who wanna match the same thing as JavaScript does:
[-+]?(\d+\.?\d*|\.\d+)
Matches:
1
+1
-1
0.1
-1.
.1
+.1
Drawing: https://regexper.com/#%5B-%2B%5D%3F%28%5Cd%2B%5C.%3F%5Cd*%7C%5C.%5Cd%2B%29
How can I create a regex that detects any string starting with two consecutive numbers followed by at the most nine consecutive characters with a hyphen symbol in java regex? For instance:
nnccccccccc-nnccccccccc
or
nncccccc-nnccccccccc
or
nnccccccc-nncccccccc
Where n represents a number from 0 to 1 and c a letter character.
So far I tried this: https://regex101.com/r/a1eJvY/2.
You may use this regex for your matches:
^\d{1,2}[a-zA-Z]{1,9}-\d{1,2}[a-zA-Z]{1,9}$
RegEx Demo
If you are using .matches() method then ^ and $ are not needed.
\d{1,2}: Match 1 or 2 digits
[a-zA-Z]{1,9}: Match 1 to 9 English letters
-: Match literal hyphen
You can use ^(\d{2}[a-zA-Z]{0,9})-(\d{2}[a-zA-Z]{0,9})$ example: https://regex101.com/r/A2wiHH/2.
This will match the string as described below:
The beginning of your string
2 decimals
0-9 characters
-,
2 decimals again,
0-9 characters again
The end of your string
I suppose that "nn" and no one letter char is accepted, so, for a single sequence:
[0,1]{2}\D{0,9}
Explaination:
[0,1]{2} --> Accept only 0 and 1 as number exactly two times;
\D{0,9} --> Accept 0 to 9 generic number.
Edit: you said
Where n represents a number from 0 to 1
but if 22may is accepted, you want number from 0 to 9, so you have to use \d
\d{2}\D{0,9}
Try this
[0-1] {2}+ [a-z] {9}. +-
Im trying to create a regex of numbers where 7 should appear atleast once and it shouldn't include 9
/[^9]//d+
I'm not sure how to do make it include 7 at least once
Also, it fails for the following example
123459, it accepts the string, even tho, there is a 9 included in there
However, if my string is 95, it rejects it, which is right
Code
Method 1
See regex in use here
(?=\d*7)(?!\d*9)\d+
Method 2
See regex in use here
\b(?=\d*7)[0-8]+\b
Note: This method uses fewer steps (170) as opposed to Method 1 with 406 steps.
Alternatively, you can also replace [0-8] with [^9\D] as seen here, which is basically saying don't match 9 or \D (any non-digit character).
You can also use \b(?=[^7\D]*7)[0-8]+\b as seen here, which brings the number of steps down from 170 to 147.
Method 3
See regex in use here
\b[0-8]*7[0-8]*\b
Note: This method uses few steps than both methods above at 139 steps. The only issue with this regex is that you need to identify valid characters in multiple locations in the pattern.
Results
Input
**VALID**
123456780
7
1237412
**INVALID**
9
12345680
1234567890
12341579
Output
Note: Shown below are strings that match.
123456780
7
1237412
Explanation
Method 1
(?=\d*7) Positive lookahead ensuring what follows is any digit any number of times, followed by 7 literally
(?!\d*9) Negative lookahead ensuring what follows is not any digit any number of times, followed by 9 literally
\d+ Any digit one or more times
Method 2
\b Assert the position as a word boundary
(?=\d*7) Positive lookahead ensuring what follows is any digit any number of times, followed by 7 literally
[0-8]+ Match any character present in the set 0-8
\b Assert the position as a word boundary
Method 3
\b Assert the position as a word boundary
[0-8]* Match any digit (except 9) any number of times
7 Match the digit 7 literally
[0-8]* Match any digit (except 9) any number of times
\b Assert the position as a word boundary
One way to do it would be to use several lookaheads:
(?=[^7]*7)(?!.*9)^\d+$
See a demo on regex101.com.
Note that you need to double escape the backslashes in Java, so that it becomes:
(?=[^7]*7)(?!.*9)^\\d+$
This has got a bit complex but it works for your use case :
(?=.*^[0-68-9]*7[0-68-9]*$)(?=^(?:(?!9).)*$).*$
First expression matches exactly one occurence of 7, accepts just numbers and second expression tests non-occurence of 9.
Try here : https://regex101.com/r/5OHgIr/1
If I find out correctly, you need a regex that accept all numbers that include at least one 7 and exclude 9. so try this:
(?:[0-8]*7[0-8]*)+
If you want found only numbers in a normal text add \s first and last of regex.
Pattern p = Pattern.compile("(?=[1-9][0-9]{2})[0-9]*[05]");
Matcher m = p.matcher("101");
while(m.find()){
System.out.println(m.start()+":"+ m.end()+ m.group());
}
Output------ >> 0:210
Please let me know why I am getting output of m.group() as 10 here.
As far as I understand m.group() should return nothing because [05] matches to nothing.
Your Pattern, (?=[1-9][0-9]{2})[0-9]*[05] consists of 2 parts:
(?=[1-9][0-9]{2})
and
[0-9]*[05]
The first part is a zero-width positive lookahead which searches for a number of length 3, and the first can not be 0. This matches your 101.
The second part searches for any amount of numbers and then a 0 or a 5. This matches the first 2 characters of 101, thus the result is 10.
See Java - Pattern for more information.
What your Regex is looking for is:
[1-9]:
match a single character present in the list below
1-9 a single character in the range between 1 and 9
[0-9]{2}:
match a single character present in the list below
Quantifier: {2} Exactly 2 times
0-9 a single character in the range between 0 and 9
[0-9]*:
match a single character present in the list below
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
0-9 a single character in the range between 0 and 9
[05]:
match a single character present in the list below
05 a single character in the list 05 literally
for the String "101" this nacht the first 2 chars 101,
so you are printing.out:
System.out.println(**m.start()**+":"+ **m.end()**+ m.group());
where m.start() returns the start index of the previous match(char at
0). where m.end() returns the offset after the last character matched.
and where m.group() returns the input subsequence matched by the previous
match.
That regex was meant to match a number that's a multiple of 5 and greater than or equal to 100, but it's useless without anchors. It should be:
^(?=[1-9][0-9]{2}$)[0-9]*[05]$
The anchors ensure that both the lookahead and the main part are examining the whole of the string. But the task doesn't require a lookahead anyway; this works just fine:
^[1-9][0-9][05]$
As #AlanMoore states, there has to be an alignment.
Assertions are a self contained entity, all they have to do is Pass
to advance to the next construct.
Lets see what (?=[1-9][0-9]{2}) matches;
1111111110666
2222222222222222225666
33333333333333333333333330666
So far so good, on to the next construct.
Lets see what [0-9]*[05] matches.
What ever this matches is the final answer.
1111111110666
2222222222222222225666
33333333333333333333333330666
What to learn is that to get a cohesive answer, assertions have to be crafted to
coincide with constructs that come after them.
Here is an example of a constraint that could be applied
after the assertion.
The assertion state's it needs three digits and the first digit must be >= 1.
The constructs after the assertion state it can be any number of digit's,
as long as it ends with a 0 or 5.
This last part is distressing since it will match only the 500000
So for sure, you need at least three digits.
That can be done like this:
[0-9]{2,}[05]
This says two things
There must be at least three digits, but can be more
It must end with a 0 or 5.
That's it, put it all together, its:
(?=[1-9][0-9]{2})[0-9]{2,}[05]
Of course, this can be condensed to;
[1-9][0-9]+[05]
I'm trying to create a regular expression in Java to validate a number with the following constraints:
The number can be of any length but can only contain digits
First digit can be 0 - 9
Subsequent digits can be 0 - 9, but one of the digits must be non-zero.
For example: 042004359 is valid, but 0000000000 is not.
\\d+[1-9]\\d* should work, I'd think.
This should do what you need:
/^(?=.*[1-9])([0-9]+)$/
Whilst it matches all of digits [0-9] it contains a lookahead that makes sure there is at least one of [1-9].
I am fairly certain that Java allows can use lookaheads.
EDIT: This regular expression test page seems to imply that it can.
EDIT: If 0 is valid, then you can use this:
^((?=.*[1-9])([0-9]+)|0)$
This will make an exception for 0 on its own (notice the OR operator).
^(\d{1})(\d*?[1-9]{1}\d*)*$
^(\d{1}) - Line must start with 1 digit
(\d*?[1-9]{1}\d*)*$ - Line must end with zero or more 0-9 digits(? for conservative), then 1 1-9 digit, then zero or more digits. This pattern can repeat zero or more times.
Works with:
100000
100100
1010200
1
2
Maybe this is too complicated, lol.
Here's one solution using lookarounds: (?<=\D|^)\d+(?=[1-9])\d*
(?<=\D|^) # lookbehind for non-digit or beginning of line
\d+ # match any number of digits 0-9
(?=[1-9]) # but lookahead to make sure there is 1-9
\d* # then match all subsequent digits, once the lookahead is satisfied