I want to match the amount of money that is at the very last portion of the string. Some amount of money are in thousands e.g. 1,200.00 and some are only hundreds, e.g. 450.95. The string looks like this "March6March7Globe-Gmovies3dTaguigCity320.00".
Once match, I want to replace the entire string by just the matching value. E.g.
March6March7Globe-Gmovies3dTaguigCity320.00
March6March7Globe-Gmovies3dTaguigCity1,320.00becomes
320.00
1,320.00Why is my pattern only matching the thousands matching and not the hundreds? Thanks.
Here's my pattern:
(.*)(\d{1}?\,?\d{3}\.\d{2}) 3 2 Answers
Please try: (.*?)((\d,)?\d{3}\.\d{2})
Here, the (*.?) is like (.*), but minimally (the shortest matching string).
The comma doesn't have to be escaped.
- It doesn’t make sense to say
\d{1}?. You want to match one digit, or none, do just say\d?. - So you could use
(.*)(\d?\,?\d{3}\.\d{2}). But that would match the2019inCity2019.00. (It would also match the,243.56inCity,234.56, if you ever got malformed input like that.) You want to matchNNN.NNorN,NNN.NN— you either haveN,or you don’t. So try(.*)((\d\,)?\d{3}\.\d{2}), which makes\d\,(i.e.,N,) a group and applies the?to it.