I have a simple RegEx that was supposed to look for 8 digits number:
String number = scanner.findInLine("\\d{8}");
But it turns out, it also matches 9 and more digits number.
How to fix this RegEx to match exactly 8 digits?
For example: 12345678 should be matched, while
1234567, and 123456789 should not.
I think this is simple and it works:
String regEx = "^[0-9]{8}$";
^ - starts with
[0-9] - use only digits (you can also use \d)
{8} - use 8 digits
$ - End here. Don't add anything after 8 digits.
Your regex will match 8 digits anywhere in the string, even if there are other digits after these 8 digits.
To match 8 consecutive digits, that are not enclosed with digits, you need to use lookarounds:
String reg = "(?<!\\d)\\d{8}(?!\\d)";
See the regex demo
Explanation:
(?<!\d) - a negative lookbehind that will fail a match if there is a digit before 8 digits
\d{8} 8 digits
(?!\d) - a negative lookahead that fails a match if there is a digit right after the 8 digits matched with the \d{8} subpattern.
Try this:
\b is known as word boundary it will say to your regex that numbers end after 8
String number = scanner.findInLine("\\b\\d{8}\\b");
Related
I am looking for a 15 characters length regex with a decimal.
In the swift documentation, the regex would look like this : 3!a15d where 3!a means [a-zA-Z]{3} and 15d means a decimal of 15 characters length with a comma.
I tried the regex below :
([A-Z]{3}[0-9]{1,14}[,][0-9]{1})|([A-Z]{3}[0-9]{1,13}[,][0-9]{1,2})|([0-9]{1,12}[,][0-9]{1,3})|([0-9]{1,11}[,][0-9]{1,4})|([0-9]{1,10}[,][0-9]{1,5})|([0-9]{1,9}[,][0-9]{1,6})|([0-9]{1,8}[,][0-9]{1,7})|([0-9]{1,7}[,][0-9]{1,8})|([0-9]{1,6}[,][0-9]{1,9})|([0-9]{1,5}[,][0-9]{1,10})|([0-9]{1,4}[,][0-9]{1,11})|([0-9]{1,3}[,][0-9]{1,12})|([0-9]{1,2}[,][0-9]{1,13})|[0-9]{1}[,][0-9]{1,14}
But it didn't work.
Do you have any tips to help me?
You can use
^[a-zA-Z]{3}(?=[^,]*,[^,]*$)\d(?:,?\d){14}$
See the regex demo.
Details:
^ - start of string
[a-zA-Z]{3} - three ASCII letters
(?=[^,]*,[^,]*$) - only one obligatory comma must be present further in the string
\d - a digit
(?:,?\d){14} - fourteen repetitions of an optional comma and a digit
$ - end of string.
Sample usage in Java to validate a string:
Boolean isValid = text.matches("[a-zA-Z]{3}(?=[^,]*,[^,]*$)\\d(?:,?\\d){14}");
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
Can someone help me with the following Java regex expression? I've done some research but I'm having a hard time putting everything together.
The regex:
"^-?\\d+$"
My understandning of what each symbol does:
" = matches the beginning of the line
- = indicates a range
? = does not occur or occurs once
\\d = matches the digits
+ = matches one or more of the previous thing.
$ = matches end of the line
Is the regex saying it only want matches that start or end with digits? But where do - and ? come in?
- only indicates a range if it's within a character class (i.e. square brackets []). Otherwise, it's a normal character like any other. With that in mind, this regex matches the following examples:
"-2"
"3"
"-700"
"436"
That is, a positive or negative integer: at least one digit, optionally preceded by a minus sign.
Some regex is composed, as you have now, the correct way to read your regex is :
^ start of word
-? optional minus character
\\d+ one or more digits
$ end of word
This regex match any positive or negative numbers, like 0, -15, 558, -19663, ...
Fore details check this good post Reference - What does this regex mean?
"^-?\\d+$" is not a regex, it's a Java string literal.
Once the compiler has parsed the string literal, the string value is ^-?\d+$, which is a regex matching like this:
^ Matches beginning of input
- Matches a minus sign
? Makes previous match (minus sign) optional
\d Matches a digit (0-9)
+ Makes previous match (digit) match repeatedly (1 or more times)
$ Matches end of input
All-in-all, the regex matches a positive or negative integer number of unlimited length.
Note: A - only denotes a range when inside a [] character class, e.g. [4-7] is the range of characters between '4' and '7', while [3-] and [-3] are not ranges since the start/end value is missing, so they both just match a 3 or - character.
This question already has an answer here:
Restricting character length in a regular expression
(1 answer)
Closed 4 years ago.
Regex: ^[a-zA-Z]+(?:[\\s'.-]*[a-zA-Z]+)*$
I want add another validation on it i.e. minimum 3 characters and maximum 15 characters.
Regex: ^([a-zA-Z]+(?:[\\s'.-]*[a-zA-Z]+)*){3,28}$
This is validating for minimum characters but not for maximum characters.
Any help is appreciated.
You could use a positive lookahead (?=.{3,15}$ to check if the string has a length from 3 - 15 characters.
Because the minimum length of the string is 3 and has to start and end with a-zA-Z you can combine the 2 character classes in the middle in this case.
I think your pattern could be simplified by removing the repetition of the group due to the positive lookahead to:
^(?=.{3,15}$)[a-zA-Z]+[\\s'.a-zA-Z-]*[a-zA-Z]+$
Explanation
^ Start of the string
(?=.{3,15}$) Positive lookahead to assert the lenght 3-15
[a-zA-Z]+ Match 1+ times a lower/upper case char a-z
[\\s'.a-zA-Z-]* Charater class to match any of the listed 0+ times
[a-zA-Z]+ Match 1+ times a lower/upper case char a-z
$ End of the string
See the Java demo
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}. +-
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.