Re: [RESOLVED] Regex problem
Quote:
Bulldog, you didn't even try to test the solution you proposed. It strips all the numbers from the string that is passed.
What I was showing you was how to match the number sequence you require thinking that you would use this to do a match. So rather than using a "split" which generates an array of segments to sort through, you could use a "match" and generate a match collection. It was tested in RegExBuddy btw.
You are currently separating out the string sequence you want by removing anything that isnt a "." or a number. This may work today, but for future proofing I would be inclined to match what you "know" to be there, rather than removing the "unknowns" around it. In the future, the database may end up with "Sold,3kilos,at,$125k" or "Note:-Refund,-35$" etc.
Over time you can easily make a regex match more specific, to deal with negative numbers you can add an optional "-" at the start of the expression, you could also deal with extra spaces, different currency symbols, or the use of a trailing "k" for thousand etc. etc. It would be extremely difficult to do this correctly by removing the characters you dont expect.
About half of my code is always consumed by error checking, a good mantra being assume nothing! and expect the unexpected!
Anyway just my $0.02...