user489041
user489041

Reputation: 28294

Regex: How not to match a few letters

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

Upvotes: 2

Views: 112

Answers (2)

Madbreaks
Madbreaks

Reputation: 19539

Why not something as simple as:

.*HUD\-1(.*+)

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:

001 001 .31412341234123

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.

Upvotes: 1

Peter Elliott
Peter Elliott

Reputation: 3322

this change will make the second match group of the regex include the full number you would like (everything after the first 1), and put the possible HUD-1 in a separate matching group, if present.

(HUD-1)?((?:-|\()?\$?(?:\d{1,3}[ ,]?)*(?:\.\d+)\)?) 

Upvotes: 0

Related Questions