Problem in regex for decimal number - java

I want a regex for decimal numbers like 00.0
I tried this [0-9]{1,2}(.[0-9]{1})? which works perfectly.
But I have to add ^ at begining and *$ at end.
Is there any way to have the regex work as the one working along with adding these characters?
^([0-9]{1,2}(.[0-9]{1})?)*$ --> fails to do what I want.
My regex should look like ^[Anything here]*$
Any help would be appreciated.
Thanks

Depends on the type of regex, but for many regex types (posix, posix extended, perl, python, emacs) . (dot) means match any symbol. To match the dot symbol you need to quote it like \..
And to match exactly one digit you don't need to add {1} at the end of it. I.e. [0-9]{1} is the same as [0-9].

I think you need .* at the end
but could you reply with some examples of strings you want to match and ones you don't want to match>

If I understand well what you need, have a try with :
\^\d\d?(\.\d)?\*\$
This will match
\^ a carret ^
\d\d? 1 or 2 digit
(\.\d)? eventually a dot and a digit
\* an asterisk
\$ a dollar

I figured out the problem was * and it could be excluded by adding a pair of parenthesis before * like ()*
And ^([0-9]{1,2}(\.[0-9])?)()*$ works well.

Related

Check if String ends with two digits after a dot in Regular Expression?

I'm trying to test if a String ends with EXACTLY two digits after a dot in Java using a Regular Expression. How can achieve this?
Something like "500.23" should return true, while "50.3" or "50" should return false.
I tried things like "500.00".matches("/^[0-9]{2}$/") but it returns false.
Here is a RegEx that might help you:
^\d+\.\d{2,2}$
it may neither be perfect nor the most efficient, but it should lead you in the right direction.
^ says that the expression should start here
\d looks for any digit
+ says, that the leading \d can appear as often as necessary (1–infinity)
\. means you are expecting a dot(.) at one point
\d{2,2} thats the trick: it says you want 2 and exactly 2 digits (not less not more)
$ tells you that the expression ends there (after the 2 digits)
in Java the \ needs to be escaped so it would be:
^\\d*\\.\\d{2,2}$
Edit
if you don't need digits before the dot (.) or if you really don't care what comes before the dot, then you can replace the first \d+ by a .* as in Bohemians answer. The (non escaped) dot means that the expression can contain any character (not only digets). Then even the leading ^ might no longer be necessary.
\\.*\\.\\d{2,2}$
use this regex
String s="987234.42";
if(Pattern.matches("^\\d+(\\.\\d{2})$", s)){ // string must start with digit followed by .(dot) then exactly two digit.
....
}
Firstly, forward slashes are no part of regular expressions whatsoever. They are however used by some languages to delimit regular expressions - but not java, so don't use them.
Secondly, in java matches() must match the whole string to return true (so ^ and $ are implied in the regex).
Try this:
if (str.matches(".*\\.\\d\\d"))
// it ends with dot then 2 digits
Note that in java a bash slash in a regex requires escaping by a further back slash in a string literal.

Regex for phone numbers with * in it

I have created a regex for phone numbers as follows \\d+ ?\\w{0,9} ?\\d+ . Now i have a problem that this only accepts numbers. Sometimes i receive the phone number as starified
so it can be 011***1334. How can i incorporate the stars portion into the above regex expression.
I'm having trouble getting your regex to work in the first place, without stars,
but anyway... you can represent the star by escaping it.
\*
So you should just turn all of your \d into [\d\*] or [\\d\\*] if you have to escape the \ first in your java.
Some regular expression engines don't require you to escape all special characters in [] so I'd watch for that behavior if it doesn't work at first
Anywhere you use \\d, turn it into [\\d\\*]
[\\d\\*]+ ?\\w{0,9} ?[\\d\\*]+
I agree with Sam I am though; the original seems odd. If you just need numbers and asterisks, this should do (escaped Java-style):
[\\d\\*]{7,10}

Regex Query in Java Program

^[0-9]\\d*(\\.\\d+)?$
I can't quite work out what the above regex pattern is looking for. I'm tempted to interpret it as "find anything that is not the numbers 0-9 inclusive, then find zero or more occurrences of a single digit, then find zero or one occurrences of a decimal point followed by at least one digit" but I'm not sure.
Part of my confusion stems from the fact that in the SCJP6 certification book, the not operator is included inside the square brackets, whereas here it's outside. Also, I am just generally inexperience when it comes to regex.
Can someone please help? [This is from a Java program. Is the above in any way Java specific?] Thanks.
^ start of a string
[0-9] a single digit
\\d* any amount of digits (0-infinity)
(\\.\\d+)? Once, or not at all: a dot followed by at least one digit
$ end of string.
You have a complicated regex that will match any floating point or non floiting point number.
Have a look at the java.util.Pattern class and and the Oracle Java Regex Tutorial.
It is looking a one or more digits, optionally followed by a . and one or more digits. It is confusing as it is needlessly complicated. It is the same as
^\\d+(\\.\\d+)?$
\d is defined as A digit: [0-9]
When the "^" operator is outside of a character class "[]" it denotes the start of input, "$" defines end of input.
So your description is correct, but it should be changed to:
find a single digit from zero to nine...
for more information about regular expressions check out this link

validating input string "RX-EZ12345678912345B" using regex

I need to validate input string which should be in the below format:
<2_upper_case_letters><"-"><2_upper_case_letters><14-digit number><1_uppercase_letter>
Ex: RX-EZ12345678912345B
I tried something like this ^[IN]-?[A-Z]{0,2}?\\d{0,14}[A-Z]{0,1} but its not giving the expected result.
Any help will be appreciated.
Thanks
Your biggest problem is the [IN] at the beginning, which matches only one letter, and only if it's I or N. If you want to match two of any letters, use [A-Z]{2}.
Once you fix that, your regex will still only match RX-E. That's because [A-Z]{0,2}? starts out trying to consume nothing, thanks to the reluctant quantifier, {0,2}?. Then \d{0,14} matches zero digits, and [A-Z]{0,1} greedily consumes the E.
If you want to match exactly 2 letters and 14 digits, use [A-Z]{2} and \d{14}. And since you're validating the string, you should end the regex with the end anchor, $. Result:
^[A-Z]{2}-[A-Z]{2}\d{14}[A-Z]$
...or, as a Java string literal:
"^[A-Z]{2}-[A-Z]{2}\\d{14}[A-Z]$"
As #nhahtdh observed, you don't really have to use the anchors if you're using Java's matches() method to apply the regex, but I recommend doing so anyway. It communicates your intent better, and it makes the regex portable, in case you have to use it in a different flavor/context.
EDIT: If the first two characters should be exactly IN, it would be
^IN-[A-Z]{2}\d{14}[A-Z]$
Simply translating your requirements into a java regex:
"^[A-Z]{2}-[A-Z]{2}\\d{14}[A-Z]$"
This will allow you to use:
if (!input.matches("^[A-Z]{2}-[A-Z]{2}\\d{14}[A-Z]$")) {
// do something because input is invalid
}
Not sure what you are trying to do at the beginning of your current regex.
"^[A-Z]{2}-[A-Z]{2}\\d{14}[A-Z]$"
The regex above will strictly match the input string as you specified. If you use matches function, ^ and $ may be omitted.
Since you want exact number of repetitions, you should specify it as {<number>} only. {<number>,<number>} is used for variable number of repetitions. And ? specify that the token before may or may not appear - if it must be there, then specifying ? is incorrect.
^[A-Z]{2}-[A-Z]{2}\\d{14}[A-Z]$
This should solve your purpose. You can confirm it from here
This should solve your problem. Check out the validity here
^[A-Z]{2}-[A-Z]{2}[0-9]{14}[A-Z]$
^([A-Z]{2,2}[-]{1,1}[A-Z]{2,2}[0-9]{14,14}[A-Z]{1,1}){1,1}$

How can I include a minus sign in this regex?

I'm trying to match the following three lines:
usemtl ftw
kd 1.2 3.2 3.1
v -12.1892 -53.4267 -276.4055
My regex matches the first two:
^(\w+) ((\S+)( \S+)*) *$
I've tried a few variants to match the negative numbers, but they just stop anything from being matched:
^(\w+) (([\S-]+)( [\S-]+)*) *$
^(\w+) (((\S|-)+)( (\S|-)+)*) *$
What am I supposed to do here? - isn't a special character in regex, is it?
- is only a special character in character classes [...]
Your problem comes from v -12.1892 -53.4267 -276.4055 containing 2 spaces in between v and -12.18.... Your regex only matches one.
Try this regex instead:
^(\w+)\s*((\S+)( \S+)*) *$
Although your regex could be simplified to (not sure exactly what you want to match and capture though):
^(\w+)(\s*\S+)*$
See it on http://rubular.com/r/D86njdYzJF
Put it first in the class: [-\S]
Then it should work.
There are two spaces between v and -12.1892 that seems to be your problem. Also to use - inside a character class i.e. [...] you need to escape it with \-
The reason why it isn't matching is because your third line has two spaces between the v and -12.1892. Try this:
^(\w+) +(([\S]+)( [\S]+)*) *$ (the added + sign allows for multiple spaces)
Here is the jsfiddle to test it: http://jsfiddle.net/xewys/
The most basic regex I could think of to match your sample data was "(\S+\s+)+", but that might not be suitable for you - it seems too generic.

Categories

Resources