Mo Is
Mo Is

Reputation: 27

Regex for random number

Well I got like tons of lines like this:

Line 60166: £5.99
Line 60294: £59.99
Line 60493: £5.53
Line 60619: £5.19
Line 60829: £5.88
Line 60847: £5.18
Line 61508: £5.98
Line 61771: £5.27
Line 61777: £5.99
Line 61789: £5.49
Line 61893: £5.00
Line 61899: £5.49
Line 61940: £500.91

I want to search for lets say only £500+ So is there an expression like to search £5[AnyNum][AnyNum]? I know its possible I done it before but I forgot what it was

Upvotes: 0

Views: 11939

Answers (4)

sixtytrees
sixtytrees

Reputation: 1233

If you need values greater than £500.00, including £12345.67 you need to account for two cases:

Number is between 500.00 and 999.99 and number is any 3+ digits. This regex should work.

  /(\d+): £[5-9]\d{2,}|(\d+): £[1-9]\d{3,}/g

To match dot and fractional part use:

 (£[5-9]\d{2,}|£[1-9]\d{3,})(\.)([0-9])([0-9])

This will pick up £1000.000 or £1000.00.00. Next expression checks for natural ending of price (space or [., ,, ;, ;, ', "] followed by space). This regex might miss some strings. For instance, string like £1000.00+ will be excluded.

 (£[5-9]\d{2,}|£[1-9]\d{3,})(\.)([0-9])([0-9])($| |[,.;':'"][ ])

https://regex101.com/r/wO7cZ2/3

Use second string if you are OK with some false positives. Expand third string to deal with false positive at a risk of false negative.

Upvotes: 0

Andrew
Andrew

Reputation: 7880

This will give you the value with all its decimals with the '£' included (I'm supposing decimals are optional):

£([5-9]\d{2,}|[1-9]\d{3,})(\.\d{1,})*

If you don't want the pound sign:

(?<=£)([5-9]\d{2,}|[1-9]\d{3,})(\.\d{1,})*

Upvotes: 0

Nick
Nick

Reputation: 10143

Yes, try using [5-9] for thundred's digit and [1-9] for thousands+ digit:

/£0*([5-9]\d*|[1-9]\d+)\d{2}/g

Upvotes: 0

baao
baao

Reputation: 73251

You should use a different approach to parsing numbers maybe, but the following regex should do what you want:

/£[5-9]\d{2,}|£[1-9]\d{3,}/g

DEMO

To get the line number, not the amount:

/(\d+): £[5-9]\d{2,}|(\d+): £[1-9]\d{3,}/g

DEMO2

Version to match decimals:

/£[5-9]\d{2,}\.\d{2}|£[1-9]\d{3,}\.\d{2}/g

Upvotes: 1

Related Questions