Reputation: 305
I'm trying to get all instances of a £values from a string. eg the offer would be from £12,000 to £15,000.05 over 6 years with the allowance of 32 extras
would return 12,000
and 15,000.05
I think I want regex to say 'get all values between £ and white space' or perhaps 'get all values between £ that are numbers, then there may or may not be a . then there may or may not be another number then there will be white space'
I've found this Extract dollar amount from string - regex in PHP
which I've turned into:
<cfset mystring = 'the offer would be from £12,000 to £15,000.05 over 6 years with the allowance of 32 extras'>
<cfset valuesfound = rematch('/\£([0-9]+[\.]*[0-9]*)/', mystring) >
But alas valuesfound is coming up as an empty array.
Any help would be very grateful received.
Upvotes: 1
Views: 295
Reputation: 5510
<cfset mystring = 'the offer would be from £12,000 to £15,000.05 over 6 years with the allowance of 32 extras'>
<cfset valuesfound2 = ReMatch("(\£[0-9,\.]*)", mystring)>
<cfdump var="#valuesfound2#">
Edit: I forgot to account for decimals, fixed.
Upvotes: 0
Reputation: 43286
(?<=£)\d+(,?\d{3})*(\.\d+)?
This will only match correctly formatted numbers. See demo.
Explanation:
(?<=£) # assert there's a "£" here
\d+ # consume as many digits as possible
(,?\d{3})* # consume a "," followed by exactly 3 digits, any number of times.
(\.\d+)? # finally, match "." followed by at least one digit - if possible.
Upvotes: 0
Reputation: 56829
A lax regex to capture the numbers would be:
£(\d[\d,.]*)
The number is available in capturing group 1.
This just check that there is a digit after the pound sign, and the rest can be any number of digits or decimal point .
or thousand separator ,
.
Note that £3.5,5.2
or £3...,,,,2
or £3.
or £3,
can be matched by the regex above. But at least, it is somewhat more reasonable than getting £-sign
as a match.
The lax regex above is only used for extracting information though. Don't use it for validation.
If you want a stronger regex:
£((?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d+)?)
Example of the most crazy things that this regex can match:
£1
£0000
£0000.00000
£0.33
£0.234235234
£0001340590783445
£0.4
£234,444.2344
£789
£59.2
The thousand separator is optional, and if it is there, it must separate the whole part into group of 3's. The decimal part is optional, and allow arbitrary number of digits.
Upvotes: 0
Reputation: 174826
I want regex to say 'get all values between £ and white space'
£(\S+)
OR
(?<=£)\S+
+
would repeats the previous character one or more times, so you can't get any empty string in the array list.
Upvotes: 1