Range of characters in regex [duplicate] - java

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

Related

Given string filter out unique element from string using regex

I have this String and I want to filter the digit that came after the big number with the space, so in this case I want to filter out 2 and 0.32. I used this regex below which only filters out decimal numbers, however I want to filter both decimals and integer numbers, is there any way?
String s = "ABB123,ABPP,ADFG0/AA/BHJ.S,392483492389 2,BBBB,YUIO,BUYGH/AA/BHJ.S,3232489880 0.32"
regex = .AA/BHJ.S,\d+ (\d+.?\d+)
https://regex101.com/r/ZqHDQ8/1
The problem is that \d+.?\d+ matches at least two digits. \d+ matches one or more digits, then .? matches any optional char other than line break char, and then again \d+ matches (requires) at least one digit (it matches one or more).
Also, note that all literal dots must be escaped.
You can use
.AA/BHJ\.S,\d+\s+(\d+(?:\.\d+)?)
See the regex demo.
Details:
. - any one char
AA/BHJ\.S, - a AA/BHJ.S, string
\d+ - one or more digits
\s+ - one or more whitespaces
(\d+(?:\.\d+)?) - Group 1: one or more digits, and then an optional sequence of a dot and one or more digits.
You could look for anything following /AA/BHJ with a reluctant quantifier, then use a capturing group to look for either digits or one or more digits followed by a decimal separator and other digits.
/AA/BHJ.*?\s+(\d+\.\d+|\d+)
Here is a link to test the regex:
https://regex101.com/r/l5nMrD/1

Having difficulty understanding Java regex interpretation [duplicate]

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.

RegEx pattern to detect two consecutive numbers followed by at the most 9 characters?

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}. +-

Java RegEx that matches exactly 8 digits

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");

Java-flavoured Regex : Match whole string if group is n chars

I'm trying to create a Regex for a String validator. My String must be exactly 8 characters long, and begin with a letter (lowercase or uppercase) or a number. It can only contain letters (lowercase and uppercase), numbers or whitespaces right after that first character. If a whitespace is found, there can only be whitespaces after it.
For now, I have the match group for the second part : [a-zA-Z0-9]{1,}\s*
I can't find a way to specify that this group is matched only if it has exactly 8 characters. I tried ^([a-zA-Z0-9]{1,}\s*){8}$ but this is not the expected result.
Here are some test cases (with trailing whitespaces).
Valid :
9013
20130
89B
A5000000
Invalid :
9013
20130
90 90
123456789
There probably is a smart regex way to do it but you could also first check the length of the string:
input.length() == 8 && input.matches("[a-zA-Z0-9]+\\s*")
This is also probably more efficient than a complex regex.
You can use this lookahead based regex:
^[a-zA-Z0-9](?!.* [a-zA-Z0-9])[a-zA-Z0-9 ]{7}$
RegEx Demo
^[a-zA-Z0-9] matches an alpha-num char at start
(?!.* [a-zA-Z0-9]) is negative lookahead to make sure that there is no instance of an alpha-num char followed by a space.
[a-zA-Z0-9 ]{7}$ matches 7 chars containing alpha-num char or space.

Categories