I have the following string: SEE ATTACHED ADDENDUM TO HUD-1194,520.07
Inside that string is HUD-1 and after that is 194,520.07. What I want is the 194,520.07 part.
I have written the following regular expression to pull that value out:
[^D\-1](?:-|\()?\$?(?:\d{1,3}[ ,]?)*(?:\.\d+)\)?
However, this pulls out: 94,520.07
I know it has something to do with this part: [^D\-1] “eating” to many of the 1‘s. Any ideas how I can stop it from “eating” 1‘s after the first one that appears in HUD-1?
UPDATED:
The reason for all the other stuff is I only want to match as well if the value after HUD-1 is a money amount. And the rest of that regex tries to determine all the different ways a money amount could be written
Why not something as simple as:
Ok, you need to be more restrictive I see based on your updated question. Try changing
[^D\-1]to just(?:HUD\-1)?. For what it’s worth, your currency RegEx is vary lax, allowing input like:You might consider not reinventing the wheel there, I’m sure you can find a currency RegEx quickly via Google. Otherwise, I’d also suggest anchoring your RegEx with a
$at the end of it.