I have a string that contains a odd money value. The string is:
Daily interest charges #901 $ 17.2789 259.18 190.07/day
The odd value is $ 17.2789
I am trying to create a regular expression that will get is, but not have a back reference. This is what I have come up with:
(?:\\$\\s*?\\d{0,1,2,3}\\.\\d{0,1,2,3,4}\\s*?/?day)?
This however will not compile. Everything in there seems reasonable to me? Any idea what might be wrong with it? I am using Java
EDIT
I have tried m.buettner suggestion of \\$\\s*\\d*\\.\\d*. I places it in the 3rd grouping below. This almost does it. Here is my full regular expression:
(.*)\s?#(\s?\d{3,4})\s*(?:\$\s*\d*\.\d*)?((?:-|\()?\$?(?:\d{1,3}[ ,]?)*(?:\.\d+)\)?)\s*((?:-|\()?\$?(?:\d{1,3}[ ,]?)*(?:\.\d+)\)?).*
The groups it pulls are:
- Daily interest charges
- 901
- 9 259.18
- 190.07
It is grouping 3 that is the issue. It contains one extra digit, the first 9
Edit Edit
There was a space that was causing the issue, this did it:
(?:\$\s*\d*\.\d*\s)?
As far as I know
\\d{0,1,2,3...}is not valid syntax. What you meant was probably:Also, are you aware that your whole pattern is optional? This poses a problem.
\\d{0,3}\\.\\d{0,4}will only match the first (odd) number. Then you have an optional slash, but a mandatoryday(if the whole pattern is not dropped). But in your input string there are a few other numbers before/dayis encountered. So even if it compiles, it won’t match your price value. You should probably leave out the\\s*?/?dayaltogether. And think about removing the outer?, too. And as Brian stated in the comment, there is no need to make the\\srepetition ungreedy, since it and the following element are mutually exclusive anyway:And do you really have to be so specific about the number of digits if the value is “odd” anyway?