Regex - pattern for allowing only numbers - java

I am trying to get a REGEX which allows only number starts from 1-9 and only of 4 characters and should can only have a zero after decimal.
This is what i have written so far, but it fails every time.
^[1-9]\\d{4,4}(\\.\\d{1,1})?%?$
I am using this as a validation in java while reading an excel sheet.

For your given requirements you may use this regex:
final String regex = "^[1-9]\\d{3}(?:\\.0)?$";
RegEx Breakup
^: Start
[1-9]: Match a digit 1-9 at start
\\d{3}: Match any 3 digits
(?:\\.0)?: Match optional .0 at the end
$: End

Related

Mixed Regular Expression for Numbers with different digits, optional letters and dots

I am working on an Android app for Handheld Scan devices that should scan different Types of QR Codes; these QR Codes can contain different variations of digits, letters, dots and plus Signs; as I am not an expert for Regular Expressions, any hints or help would be very much appreciated.
The Regular Expression(s) should match the occurences of the following digits, letters, dots and plus signs:
1256+70
1235.B+70
1256+70+DB
1235.B+70+DB
1256+70+DB2020-123
1235.B+70+DB2020-123
1256+0+DB2020-123
1235.B+0+DB2020-123
The number range of the first four digits can be [100-99999].[A-Z]
I came up with the following Regular Expressions
[0-9]{4}
[0-9]{3,6}$.?[A-Z]?+[0-9]+DB[0-9]{4}-[0-9]]
[0-9]{3,6}$.?[A-Z]?+[0-9]
[0-9][0-9][0-9][0-9].[0-9][0-9].+DB
\b\d{3,6}\b
[0-9]{3,6}$.?[A-Z]?+[0-9]+DB[0-9]{4}-[0-9]]
[0-9]{3,6}$.?[A-Z]?+[0-9]
[0-9]{3,6}$.?[A-Z]?+[0-9]
but they are not covering all of the possible combinations and missing out a lot of options - hence any help or hints would be very much appreciated - thanks in advance!
Looking at the example data, one option could be using 1 regex with specific and optional parts:
^[1-9]\d{2,4}(?:\.[A-Z])?\+\d+(?:\+DB(?:\d{4}-\d{3})?)?$
^ Start of string
[1-9]\d{2,4} Match from 100 till 99999
(?:\.[A-Z])? Optionally match . and a char A-Z
\+\d+ match + and 1+ digits (or use \d{1,2} to match 1 or 2 digits)
(?: Non cpature group
\+DB(?:\d{4}-\d{3})? match + and DB and optionally match 4 digits - and 3 digits
)? Close non capture group and make it optional
$ End of string
Regex demo
In Java
String regex = "^[1-9]\\d{2,4}(?:\\.[A-Z])?\\+\\d+(?:\\+DB(?:\\d{4}-\\d{3})?)?$";
[1-9][0-9]{2,5}[.0-9A-Z+\-]+
will work.

Regex to match lines having specific count of Decimal numbers

I want to match the lines having specific count of decimal numbers separated by spaces. Say lines having only 3 decimal numbers. Consider the below example:
Abc 1.56 1.67 5.67
xyz 4.51 12.43 32.50
03/31/2019 $1234 $(1234) $60,501 5.81 7.81
abcdf $123,345 $123 $123,149
For this given input I want to fetch only the first two lines as they have only contains 3 decimal numbers separated by space. I have tried (.*[\s0-9.]+)$ && ([\s0-9.]+)$ but with these I end up getting many other unwanted lines as well.Could some one please advise if this is something we can do using regular expression.
Using [\s0-9.]+ is a broad match for a decimal number, as it could also match only newlines, spaces or dots.
If there can also digits occur before the decimals, you could use:
^.*?\d+\.\d+(?:\h+\d+\.\d+){2}$
Explanation
^ Start of string
.*? Match any char except an newline non greedy
\d+\.\d+ Match a decial number
(?:\h+\d+\.\d+){2} Repeat 2 times matching 1+ horizontal whitespace chars and a decimal number
$ End of string
In Java
String regex = "^.*?\\d+\\.\\d+(?:\\h+\\d+\\.\\d+){2}$";
Regex demo
If the digit before the decimal is optional, you could change it to \d*\.\d+
To match lines starting with some text, then three decimal numbers separated by spaces, use this regex:
(?m)^(\p{L}+)\h+(-?[\d.]+)\h+(-?[\d.]+)\h+(-?[\d.]+)\h*$
Remember to double the \ when inserting into a Java string literal.
See regex101 for demo.

Regex to match first number only if the string starts with 3 numbers in a row

I have to make a regex which selects the first number only if there are 3 numbers in a row in the beginning of the string.
Example:
012 test
Here I'd like to match the 0 at the beginning of the string.
02 test
Here I want nothing to be matched by the regex.
I have no idea how to check the string without matching it using regex. Is this possible?
Use positive lookahead assertion to check which follows two digits and start anchor(^) to start at the beginning of the string.
/^\d(?=\d{2})/
UPDATE : You can use word boundary(\b) to avoid matching when it follows any word character after the two digits.
/^\d(?=\d{2}\b)/

Regex Exceptions

I am trying to come up with a java regex that will match numbers with 2 too 3 decimals and not match any decimal number more than 3.
this is my regex
[0-9]{2}[.][0-9]{3}
It matches 41.51778000 and 18.740
but I only want it to match numbers that have exactly 3 decimal places and not numbers with more than three
You need to ask the regex to match the end and start as well.
^[0-9]{2}[.][0-9]{3}$
You must use word boundary on either side to stop unexpected matches:
\b[0-9]{2}[.][0-9]{2,3}\b
In Java it would be:
\\b\\d{2}\\.\\d{2,3}\\b
You can invoke Matcher#matches or String.matches instead of, say, Matcher#find to match the whole String.
Otherwise, you can prepend ^ and append $ to your pattern, to delimit start and end of input.
Finally, you can surround your pattern with something like \\D, or \\b or \\w to respectively match non-digits, word boundaries or whitespace around it, if you need to invoke find on an input containing more than 1 instance of the pattern.

Regular expression to match until last 3 characters before a comma

Maybe this is asked somewhere but certainly I couldn't find the answer I want so:
I'm having difficulties to match specific characters in a string:
"88551554,86546546,51516565"
The digits I want to match are the X's in the following :
"XXXXX554,XXXXX546,XXXXX565"
Right now I'm only able to find out the last 3 digits before each comma :
\d{3}(?=,)
And since the length of the numbers are dynamic, it seems not possible to specify the number of digits before the 3 digits.
Anyone can help?
Thanks in advance!
You can use this lookahead regex:
(\d+)(?=\d{3}(?:,|$))
RegEx Demo
This will match and group 1 or more digits that must be followed by 3 digits and a comma or end of input. Check MATCH INFORMATION in the demo link for captured groups.
Update: To replace all those matched digits by X use:
str = str.replaceAll("\\d(?=\\d*\\d{3}(?:,|$))", "X");
RegEx Demo2
To match it use:
\d+(?=\d{3})
This regex does:
\d+... Match a digit (0-9) between one and unlimmited times.
(?=\d{3}) ... Match a digit (0-9) exactly three times inside an positive lookahead.

Categories