What's a good regular expression for real numbers in Java? - java

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]+)?

Related

Java Regex to match a specific string or another specific string, or not at all?

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)?)

Regular Expression to replace integers with floats

We're trying to replace integer values with float values in a String, for example:
#var1 * #var2/ 1+100 - 2 + 1.5 - .5
The regular expression should match 1, 100 and 2, but not numbers which are already floats, eg 1.5 and .5
I've gotten as far as /[^\w](\d+)/, which finds digits by themselves.
Now, how do I exclude numbers from this regular expression, that are followed by \.?\d+?
The RegEx should work in Java or Actionscript 3.
Regular Expression Test
This will work in Java: /(?<![.\w])\d+(?![.\w])/. It uses both lookahead and lookbehind to stop matching digits that are either preceeded or succeeded by a dot/letter.
Why no trying lookahead? I believe it works with Java, no clue about ActionScript.
[^\w](\d+)(?![.]\d+)/
Would match only those sequences of digits not immediately followed by a dot integer(s).
You can use a negative look ahead for this
(?<!\.\d+)
would exclude this, but you need to combine this with an anchor otherwise you will get a parital match.
/(?<!\B|\.)(\d+)(?!\.\d+)\b/
You should also change the non-word character before the digits. I used here a negative lookbehind assertion (?<!\B|\.). it ensures that there is no dot before the digit or not a non word boundary (double negation to match on a word boundary.)
See it here on Regexr
If your regex syntax supports lookahead, you can use that. Java does; not sure about AS3.
/[^\w.]\d+(?!\.)/
Note that in this case you'd want to use the entire matched string in the replacement.
My suggestion is
\b(?<!\.)\d+(?!\.)\b

Regex to match these number rules

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.

Regex for XXYXX

I'm looking for a regular expression for Java to find digits structured like this:
XXYXX or XYYYX
So results could be 66266 or 71117
Thanks for your help!
Try this regex:
input.matches("((\\d)\\1\\d\\1\\1|(\\d)(\\d)\\3\\3\\2)");
It uses back references to handle repeating numbers and the regex "or (A|B)
Note that this regex will match 99999, which is allowable by your definition (ie X and Y may be the same digit).
Also note the escaped back slashes \\ for specifying a single backslash in the regex in a java String.
I'd suggest (\d)\1\d\1\1 for the first case and (\d)(\d)\2\2\1 for the second. Mixing them both with non-capturing groups:
(?:(\d)\1\d\1\1)|(?:(\d)(\d)\2\2\1)
Not sure how Java plays with Regex though.

Regular Expression for Simple Arithmetic String

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.

Categories

Resources