Java Regex validation for list of numbers - java

I'm a newbie in java regex. i am seeking for advise for this series of number checking :
Number, must be >= 10 digits, user is not allowed to input as follows:
"0000000000","1111111111","2222222222","3333333333","4444444444",
"5555555555","6666666666","7777777777","8888888888","9999999999",
"1234567890","00000000000","11111111111","22222222222","33333333333",
"44444444444","55555555555","66666666666","77777777777","88888888888",
"99999999999"
currently my regex pattern is something like this
^(?=\\d{8,11}$)(?:(.)\\1*)$
this validates all numbers in the series except the 1234567890. any advise is appreciated. Thank you.

Use this:
^(?!(\d)\1+\b|1234567890)\d{10,}$
See what matches and fails in the Regex Demo.
To validate in Java, with matches we don't need the anchors:
if (subjectString.matches("(?!(\\d)\\1+\\b|1234567890)\\d{10,}")) {
// It matched!
}
else { // nah, it didn't match...
}
Explanation
The negative lookahead (?!(\d)\1+\b|1234567890) asserts that what follows is not...
(\d)\1+\b one digit (captured to Group 1), follows by repetitions of what was matched by Group 1, then a word boundary
OR |
1234567890
\d{10,} matches ten or more digits

Number, must be >= 10 digits, user is not allowed to input as follows.
You can use String.matches() method to check for any match.
Try below regex that checks for possible inputs as suggested by you. add more as per your need.
1234567890|(\d)\1{9}
Here is Live demo
Pattern explanation:
1234567890 '1234567890'
| OR
( group and capture to \1:
\d digits (0-9)
) end of \1
\1{9} what was matched by capture \1 (9 times)
Sample code:
String regex ="1234567890|(\\d)\\1{9}";
System.out.println("0000000000".matches(regex)); // true
System.out.println("1234567890".matches(regex)); // true
System.out.println("1111111111".matches(regex)); // true

Related

minimum number in a string should be 1 regex validation?

I have a String which I need to match. Meaning it should only contains a number followed by space or just a number and minimum number should be 1 always. For ex:
3 1 2
1 p 3
6 3 2
0 3 2
First and third are valid string and all other are not.
I came up with below regex but I am not sure how can I check for minimum number in that string should be 1 always?
str.matches("(\\d|\\s)+")
Regex used from here
Just replace \\d with [1-9].
\\d is just a shorthand for the class [0-9].
This is a better regex though: ([1-9]\\s)*[1-9]$, as it takes care of double digit issues and won't allow space at the end.
Not everything can or should be solved with regular expressions.
You could use a simple expression like
str.matches("((\\d+)\\s)+")
or something alike to simply check that your input line contains only groups of digits followed by one or more spaces.
If that matches, you split along the spaces and for each group of digits you turn it into a number and validate against the valid range.
I have a gut feeling that regular expressions are actually not sufficient for the kind of validation you need.
If it should only contains a number followed by space or just a number and minimum number should be 1 and number can also be larger than 10 you might use:
^[1-9]\\d*(?: [1-9]\\d*)*$
Note that if you want to match a space only, instead of using \s which matches more you could just add a space in the pattern.
Explanation
^ Assert the start of the string
[1-9]\\d* Match a number from 1 up
(?: [1-9]\\d*)* Repeat a number from 1 up with a prepended space
$ Assert end of the string
Regex demo
Regex is part of the solution. But I don't think that regex alone can solve your problem.
This is my proposed solution:
private static boolean isValid(String str) {
Pattern pattern = Pattern.compile("[(\\d+)\\s]+");
Matcher matcher = pattern.matcher(str);
return matcher.matches() && Arrays.stream(Arrays.stream(matcher.group().split(" "))
.mapToInt(Integer::parseInt)
.toArray()).min().getAsInt() == 1;
}
Pay attention to the mathing type: matcher.matches() - to check match against the entire input. (don't use matcher.find() - because it will not reject invalid input such as "1 p 2")

Regular Expression that matches number with max 2 decimal places

I'm writing a simple code in java/android.
I want to create regex that matches:
0
123
123,1
123,44
and slice everything after second digit after comma.
My first idea is to do something like that:
^\d+(?(?=\,{1}$)|\,\d{1,2})
^ - from begin
\d+ match all digits
?=\,{1}$ and if you get comma at the end
do nothin
else grab two more digits after comma
but it doesn't match numbers without comma; and I don't understand what is wrong with the regex.
You may use
^(\d+(?:,\d{1,2})?).*
and replace with $1. See the regex demo.
Details:
^ - start of string
-(\d+(?:,\d{1,2})?) - Capturing group 1 matching:
\d+ - one or more digits
(?:,\d{1,2})? - an optional sequence of:
, - a comma
\d{1,2} - 1 or 2 digits
.* - the rest of the line that is matched and not captured, and thus will be removed.
basic regex : [0-9]+[, ]*[0-9]+
In case you want to specify min max length use:
[0-9]{1,3}[, ]*[0-9]{0,2}
Here:
,{1}
says: exactly ONE ","
Try:
,{0,1}
for example.

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.

String validation in java using regex

I have to validate a set of strings and do stuff with it. The acceptable formats are :
1/2
12/1/3
1/23/333/4
The code used for validation is:
if (str.matches("(\\d+\\/|\\d+){2,4}")) {
// do some stuff
} else {
// do other stuff
}
But it will match any integer with or without slashes, I want to exclude ones without slashes.. How can I match only the valid patterns?
It looks like you want to find number (series of one or more digits - \d+) with one or more /number after it. If that is the case then you can write your regex as
\\d+(/\\d+)+
You can try
(\d+/){1,3}\d+
digits followed by / one to three times----^^^^^^ ^^------followed by digit
Sample code:
System.out.println("1/23/333/4".matches("(\\d+/){1,3}\\d+")); // true
System.out.println("1/2".matches("(\\d+/){1,3}\\d+")); // true
System.out.println("12/1/3".matches("(\\d+/){1,3}\\d+")); // true
Pattern explanation:
( group and capture to \1 (between 1 and 3 times):
\d+ digits (0-9) (1 or more times)
/ '/'
){1,3} end of \1
\d+ digits (0-9) (1 or more times )
\\b\\d+(/\\d+){1, 3}\\b
\b is a word boundary. This will match all tokens with 1-3 slashes, with the slashes surrounded by digits and the token surrounded by word boundaries.

How to build a regular expression for a string?

How can i write this as a regular expression?
"blocka#123#456"
i have used # symbol to split the parameters in the data
and the parameters are block name,startX coordinate,start Y corrdinate
this is the data embedded in my QR code.so when i scan the QR i want to check if its the right QR they're scanning. For that i need a regular expression for the above syntax.
my method body
public void Store_QR(String qr){
if( qr.matches(regular Expression here)) {
CurrentLocation = qr;
}
else // Break the operation
}
The Information you specified does not justice using a regular expression at all.
Try to from it in a more general way.
If you really need to scan for "blocka#123#456" then use qr.contains("blocka#123#456");
It depends on what you want to match.
Here are some regex propositions:
^blocka#[0-9]{3}#[0-9]{3}$
^blocka#[0-9]+#[0-9]+$
^blocka(#[0-9]{3}){2}$
^blocka(#[0-9]+){2}$
^blocka(#[0-9]{3})+$
^blocka(#[0-9]+)+$
Otherwise, just use contains() or similar.
myregexp.com is nice to do some testing.
Official Java Regex Tutorial is quite ok to learn and includes most things one needs to know.
The Pattern documentation also includes fancy predefined character classes that are missing in above tutorial.
You did not specify anything that has to be regular in that example you gave. Regular expressions make only sense if there are rules to validate the input.
If it has to be exactly "blocka#123#456" then "blocka#123#456" or "^blocka#123#456$" will work as regex. Stuff between ^ and $ means that the regex inside must span from begin to end of the input. Sometimes required and usually a good idea to put that around your regex.
If blocka is dynamic replace it with [a-z]+ to match any sequence of lowercase letters a through z with length of at least 1. block[a-z] would match blocka, blockb, etc.
And [a-z]{6} would match any sequence of exactly 6 letters. [a-zA-Z] also includes uppercase letters and \p{L} matches any letter including unicode stuff (e.g. Blüc本).
# matches #. Like any character without special regex meaning ( \ ^ $ . | ? * + ( ) [ ] { } ) characters match themselves. [^#] matches every character but #.
Regarding the numbers: [0-9]+ or \d+ is a generic pattern for several numbers, [0-9]{1,4} would match anything consisting out of 1-4 numbers like 007, 5, 9999. (?:0|[1-9][0-9]{0,3}) for example will only match numbers between 0 and 9999 and does not allow leading zeros. (?:STUFF) is a non-capturing group that does not affect the groups you can extract via Matcher#group(1..?). Useful for logical grouping with |. The meaning of (?:0|[1-9][0-9]{0,3}) is: either a single 0 OR ( 1x 1-9 followed by 0 to 3 x 0-9).
[0-9] is so common that there is a predefinition for it : \d (digit). It's \\d inside the regex String since you have to escape the \.
So some of your options are
".*" which matches absolutely everything
"^[^#]+(?:#[^#]+)+$" which matches anything separated by # like "hello #world!1# -12.f #本#foo#bar"
"^blocka(#\\d+)+$" which matches blocka followed by at least one group of numbers separated by # e.g. blocka#1#12#0007#949432149#3
"^blocka#(?:[0-9]|[1-9][0-9]|[1-3][0-9]{2})#[4-9][0-9]{2}$" which will match only if it finds blocka# followed by numbers 0 - 399, followed by a # and finally numbers 400-999
"^blocka#123#456$" which matches only exactly that string.
All that are regular expressions that match the example you gave.
But it's probably as simple as
public void Store_QR(String qr){
if( qr.matches("^blocka#\\d+#\\d+$")) {
CurrentLocation = qr;
}
else // Break the operation
}
or
private static final Pattern QR_PATTERN = Pattern.compile("^blocka#(\\d+)#(\\d+)$");
public void Store_QR(String qr){
Matcher matcher = QR_PATTERN.matcher(qr);
if(matcher.matches()) {
int number1 = Integer.valueOf(matcher.group(1));
int number2 = Integer.valueOf(matcher.group(2));
CurrentLocation = qr;
}
else // Break the operation
}
BlockName#start_X#start_Y any block name.. starting with the string"block" and followed by two integers
I guess a good regex for that would be "^block\\w+#\\d+#\\d+$", starting with "block", then any combination of a-z, A-Z, 0-9 and _ (thats the \w) followed by #, numbers, #, numbers.
Would match block_#0#0, blockZ#9#9, block_a_Unicorn666#0000#1234, but not block#1#2 because there is no name at all and would not match blockName#123#abc because letters instead of number. Would also not match Block_a#123#456 because of the uppercase B.
If the name part (\\w+) is too liberal (___, _123 would be a legal names) use e.g. "^block_?[a-zA-Z]+#\\d+#\\d+$", what won't allow numbers and names may only be separated by a single optional _ and there have to be letters after that. Would allow _a, a, _ABc, but not _, _a_b, _a9. If you want to allow numbers in names [a-zA-Z0-9] would be the character class to use.
I suggest:
[a-z]+#\d+#\d+
And if you want capture the 3 parts:
([a-z]+)#(\d+)#(\d+)
Matcher.group( 1, 2 or 3 ) returns the parts

Categories