How can I verify with regex in Java if a number is thousand separated (for example with dot)?
Of course it doesn't have to accept any negative number. I've already Googled all around and so far the best I found was [1-9]?\.[0-9]*. However, it's not perfect. For example it accepts 1.000000000 which is not correct.
How can I verify a positive number with a dot thousand separator? For example the number: 1.024.553 or 100.000
It should accept:
123
123.123
0
12.111
But not:
00
kukac
0.111
1...1
1.1
You could use this pattern:
^\d+|\d{1,3}(?:\.\d{3})*$
This will match any simple sequence of digits without thousands separators, or any sequence with . separators between every 3 digits. If you also want to support a comma as a thousands separator, use this:
^\d+|\d{1,3}(?:[,.]\d{3})*$
Of course, to use any of these in Java, you'll need to escape the \ characters:
String pattern = "^\\d+|\\d{1,3}(?:\\.\\d{3})*$";
Update Given your updated specs, I'd recommend this pattern:
^(?:0|[1-9][0-9]{0,2}(?:\.[0-9]{3})*)$
You can test it here: Regex Tester
Related
I need to match either letter A preceded by 1-5 digits or letter B preceded by 1-4 digits.
So my regex looks like this:
(\d{1,5}A)|(\d{1,4}B)
But this matches the last 4 digits before an A.
Any solutions?
this matches the last 4 digits before an A
Require the item before your regex to not be preceded by a digit:
(?<!\d)((\d{1,5}A)|(\d{1,4}B))
Another solution is to require a word boundary with \b.
lookahead/lookbehind tutorial
Something along the lines of:
(\d{1,5}A)|(\d{1,4}B)
I would advise taking a look at cheatsheet. if you are unfamiliar with regular expressions and try to do these kind of simple regexs yourself.
There is also plenty of online regex testing apps such as: regextester which enable you to test your regexes without writing any code.
I've written a regular expression that matches any number of letters with any number of single spaces between the letters. I would like that regular expression to also enforce a minimum and maximum number of characters, but I'm not sure how to do that (or if it's possible).
My regular expression is:
[A-Za-z](\s?[A-Za-z])+
I realized it was only matching two sets of letters surrounding a single space, so I modified it slightly to fix that. The original question is still the same though.
Is there a way to enforce a minimum of three characters and a maximum of 30?
Yes
Just like + means one or more you can use {3,30} to match between 3 and 30
For example [a-z]{3,30} matches between 3 and 30 lowercase alphabet letters
From the documentation of the Pattern class
X{n,m} X, at least n but not more than m times
In your case, matching 3-30 letters followed by spaces could be accomplished with:
([a-zA-Z]\s){3,30}
If you require trailing whitespace, if you don't you can use: (2-29 times letter+space, then letter)
([a-zA-Z]\s){2,29}[a-zA-Z]
If you'd like whitespaces to count as characters you need to divide that number by 2 to get
([a-zA-Z]\s){1,14}[a-zA-Z]
You can add \s? to that last one if the trailing whitespace is optional. These were all tested on RegexPlanet
If you'd like the entire string altogether to be between 3 and 30 characters you can use lookaheads adding (?=^.{3,30}$) at the beginning of the RegExp and removing the other size limitations
All that said, in all honestly I'd probably just test the String's .length property. It's more readable.
This is what you are looking for
^[a-zA-Z](\s?[a-zA-Z]){2,29}$
^ is the start of string
$ is the end of string
(\s?[a-zA-Z]){2,29} would match (\s?[a-zA-Z]) 2 to 29 times..
Actually Benjamin's answer will lead to the complete solution to the OP's question.
Using lookaheads it is possible to restrict the total number of characters AND restrict the match to a set combination of letters and (optional) single spaces.
The regex that solves the entire problem would become
(?=^.{3,30}$)^([A-Za-z][\s]?)+$
This will match AAA, A A and also fail to match AA A since there are two consecutive spaces.
I tested this at http://regexpal.com/ and it does the trick.
You should use
[a-zA-Z ]{20}
[For allowed characters]{for limiting of the number of characters}
I want to create a regular expression in java using standard libraries that will accommodate the following sentence:
12 of 128
Obviously the numbers can be anything though... From 1 digit to many
Also, I'm not sure how to accommodate the word "of" but I thought maybe something along the lines of:
[\d\sof\s\d]
This should work for you:
(\d+\s+of\s+\d+)
This will assume that you want to capture the full block of text as "one group", and there can be one-or-more whitespace characters in between each (if only one space, you can change \s+ to just \s).
If you want to capture the numbers separately, you can try:
(\d+)\s+of\s+(\d+)
You want this:
\d+\sof\s\d+
The relevant change from what you already had is the addition of the two plus signs. That means, that it should match multiple digits but at least one.
Sample: http://regexr.com?32cao
This regexp
"\\d+ of \\d+"
will match at least one to any number of digits, followed by string " of " followed by one to any number of digits.
I want to check if the user's input in the server side.
If the user enters a number 111111 or 22222 which has the same numbers,
and also if the input is in sequence like 12345 or 456789.
To match consecutive same digits:
^([0-9])\1*$
Note that you have to escape the backslash when you put it in a java string literal, for example,
"^([0-9])\\1*$"
For the second one you have to explicitly make a list of consecutive digits using the | operator. The regex would be really long and nasty with as many as 10-nested parantheses. One has to generate this regex using a program. In other words, this is a wrong problem to solve using regex. It would be much simpler to write a loop and test this.
This pattern will match if the user enters the same digit:
^(\d)\1*$
\1 matches the first capture group, so the pattern matches whether that first digit is repeated for the entire string.
The second problem (consecutive digits) is somewhat more difficult.
^(?:^(?:^(?:^(?:^0?1)?2)?3)4?)?5(?:$|6(?:$|7(?:$|8(?:$|90?))))$|
^(0?1)?2(?:$|3(?:$|4))|^(6?7)?8(?:$|90?)$
is one implementation, assuming three or more digits. But since the number of combinations is small, enumerating (4+ digits) is also possible:
^(?:0?123(45?)?|1?23456?|2?34567?|3?45678?|4?56789?|(5?6)?7890?|
(0?1)?2345678?(90$)?|1?23456789?|2?345678(90?)?)$
All this said, regular expressions don't always work well for this type of problem. A Java method to check for this sequence might be cleaner.
This time in perl, to explain the second case easier:
perl -nlE 'say "BAD number" if ($_ =~ /^(\d)\1*$/ or "123456789" =~ /$_/)'
Explanation:
case 1 : input ∈ /(\d)\1*/ language: already presented ($_ =~ /^(\d)\1*$/)
case 2 : string "123456789" matches input ("123456789" =~ /$_/)
I am looking for a regex java pattern to match the following string:
[Phone Number]= 1234567890
Here:
The regex should look for hardcoded string of "[Phone Number]=" followed by space or not, and followed by
any numbers of digits.
That means it should match:
[Phone Number]= 123456 and
[Phone Number]=1234567890
Any help is appreciated.
Well, something like:
String pattern = "\\[Phone Number\\]= ?\\d+";
The backslashes are doubled just because of Java string literal syntax
The square brackets are escaped to avoid them being used to group a set of characters
The ? means zero or one space
The \d+ (after unescaping) means "at least one digit"
What are your rules for a phone number?
Simply a list of digits is rarely a valid phone number format.
A simple search can find you the regexps for specific countries (I'll provide a specific one if you can tell us which one you need).