In Java, I'm currently using
str.matches("\\d")
but its only matching a single number.
I need to match ints and doubles, e.g. :
"1"
"1337"
".1"
"13.7"
Any help would be awesome.
You can try this regexp:
^(\d+(\.\d+)?|\.\d+)$
I think this regular expression can help you
\\d*\\.?\\d+
((\+|-)?(\d+(\.\d+)?|\.\d+))
This would match positive and negative ints, doubles that start with a digit, and doubles start with a dot
I think this is tidier to look at than the other suggestions, while still doing the same thing.
(\\d+)?(\\.)?\\d+
This will match any real number that the Java compiler will recognize. To do this, it also handles things like signed numbers and exponentials. It’s in Pattern.COMMENTS mode because I think anything else is barbaric.
(?xi) # the /i is for the exponent
(?:[+-]?) # the sign is optional
(?:(?=[.]?[0123456789])
(?:[0123456789]*)
(?:(?:[.])
(?:[0123456789]{0,})
) ?
)
# this is where the exponent starts, if you want it
(?:(?:[E])
(?:(?:[+-]?)
(?:[0123456789]+)
)
|
)
There is a quite extensive lesson about regular expressions in the Java Tutorials.
For information about matching multiple characters you should read the section about quantifiers.
Related
What's a good regular expression for real numbers in Java?
I created the regex ([-]?[\\d]*\\.[\\d]*) and tested it against the following expected outputs:
in out works?
--------------------------------------
qwerty34.34abcd 34.34 yes
qwe-34.34.34abcd -34.34 no
+17.-9abc 17.0 yes
-.abc0 0.0 no
I have to get out exactly what the column(out).
Please provide a regex that passes all of these tests.
Try the following:
((\+|-)?([0-9]+)(\.[0-9]+)?)|((\+|-)?\.?[0-9]+)
This will match real numbers, including integers, with or without a sign (and with or without a number before the decimal point).
The regular expression must satisfy these requirements:
There may be an optional sign. Try [+-]?.
There may be 0 or more optional digits. Try [0-9]*.
There may be a decimal point. Try [.] to avoid backslashitis.
There may be 0 or more digits after the decimal point. Try [0-9]*.
At least one of #2 and #4 has a digit. This would use [0-9]+, and it can use alternation.
Does this pattern work: [+-]?([0-9]*[.]?[0-9]+|[0-9]+[.]?[0-9]*)? I'm worried about catastrophic backtracking, however.
^[+-]?(?:\d+\.?\d*|\d*\.\d+)$
? is for making + or - optionals
?: is for non capturing group
\ is an escape char
How about the next one:
^-?(([1-9][0-9]*)|(0))(?:\.[0-9]+)?$
or the next one if you want to allow the "+" sign:
^(-|\+)?(([1-9][0-9]*)|(0))(?:\.[0-9]+)?$
BTW, here's a nice website for testing and sharing regexp.
Or this one: [0-9]+\.[0-9]*([eE][\+\-]?[0-9]+)?
Imagine capturing the input with a regex:
2.1_3_4
3.2.1
3.2.1.RELEASE
3.2.1.SNAPSHOT
The numbers and the dots are easy enough to get
([0-9\._]+)
But how do you capture that plus "RELEASE" or "SNAPHOT" or none of those?
I played around with the or operator to no avail...
([0-9\._]+RELEASE||SNAPSHOT) // no worky
btw, this is a nice regex tester: http://java-regex-tester.appspot.com/
I think you want this:
([0-9._]+(RELEASE|SNAPSHOT)?)
The (inside) parens form a group, and the question mark indicates the group may occur 0 or 1 times.
You are doing great. You just need to make a few changes.First, you do not use || for or, | is used. So RELEASE||SNAPSHOT would convert to RELEASE|SNAPSHOT.Since release or snapshot is not mandatory, a ? should be placed after it. So the final regex becomes
([0-9\._]+(RELEASE|SNAPSHOT)?)
You can also use \d instead of 0-9.
else than this, there is no need to escape . by \ when its present inside []
So finally, following could be the final regex
([\d._]+(RELEASE|SNAPSHOT)?)
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}$
I'm trying to validate a simple arithmetic expression to insure it fits within the format operand operator operand : 234.34 + 5. I figured out how to validate this easy enough, but I want the users to be able to continue inputting more than 2 values such as: 234.34 + 5 / 6 * 7 - -34. So far my regex is as follows:
[-]*\d+[.\d+[E\d+]*]*[\s+[*+/-]\s+[-]*\d+[.\d+[E\d+]*]*]*
This partially works, but the problem I have is it allows for some strange things I don't want such as -4.34.1 - 34 +
Any suggestions?
Try this. It's ugly as hell but it should work (if you aren't using any parentheses):
-?\d+(?:\.\d+(?:E\d+)?)?(\s*[-+/\*]\s+-?\d+(?:\.\d+(?:E\d+)?)?)+
Explanation
This will math a number followed by an operator and a number indefinitely
-?\d+(?:\.\d+(?:E\d+)?)? Match a number
(
\s* optional whitespace
[-+/\*] any operator: +, -, *, /
\s+ at least one whitespace (to avoid a --b)
-?\d+(?:\.\d+(?:E\d+)?)? match another number
)+ repeat this block one or more times
And the number expression:
-? optional -
\d+ digits (one or more)
(?: start of optional part
\. dot
\d+ digits
(?: start of optional scientific notation part
E match E char
\d+ match digitx
)? close of the optional scientific notatotion part
)? close optional group
But i strongly suggest trying to write a proper parser for this, it will also allow supporting of parentheses: a + (b + c).
I hate to be "that guy" but why not just write a simple validator that parses the string without using regular expressions? What's the reasoning behind using regular expressions for this? If you were to write your own parser, not only will the solution be easier to understand and maintain but with a little bit more work you would be able to evaluate the expression as well.
It may be best to just write a parser. I know, that sounds scary, but this is actually a second-year homework exercise at college.
See Dijkstra's Shunting-yard algorithm. This will allow you to both verify and evaluate the expression, so if that is where you're going with this project, you're going to have to implement it anyways...
i released an expression evaluator based on Dijkstra's Shunting Yard algorithm, under the terms of the Apache License 2.0:
http://projects.congrace.de/exp4j/index.html
Why not use string.split to get each operand and value by itself. Then you can parse it using much simpler regex ([\d*.\d*|\d|+|-|*|/]) or just Integer.getInterger for your values.
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.