RegEx to find variable dollar amount in a wall of text Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
carno
Posts: 265
Joined: 20 Jun 2014, 16:48

RegEx to find variable dollar amount in a wall of text  Topic is solved

11 Oct 2017, 15:13

I have the following sentence in a wall of text where I want to extract an embedded variable dollar amount (could be 1 to 3 digits) between [... done for] and [USD (excl. tax) but...], so in the following case this number should be [50]. What would be the most efficient way to achieve this using RegEx or other methods?

...
text
text
text
Based on past jobs we think we can get this job done for 50 USD (excl. tax) but we are not set on this as we also want ...
text
text
text
...
Last edited by carno on 11 Oct 2017, 16:13, edited 1 time in total.
magicinmath
Posts: 162
Joined: 12 Apr 2017, 23:03

Re: RegEx to find variable dollar amount in a wall of text

11 Oct 2017, 15:59

I'm just learning RegExMatch but this is what I figured out so far. Probably a more efficient way yet hopefully someone can school us both!

Code: Select all

note=
(
...
stuff here blah blahblhablhablah sklchsldkn klcnaslnk 2345 34 5 333
Based on past jobs we think we can get this job done for 5 USD (excl. tax) but we are not set on this as we also want ...
Based on past jobs we think we can get this job done for 20 USD (excl. tax) but we are not set on this as we also want ...
textBased on past jobs we think we can get this job done for 75 USD (excl. tax) but we are not set on this as we also want ...
textBased on past jobs we think we can get this job done for 500 USD (excl. tax) but we are not set on this as we also want ...
text
text
...
)

vars:=[]

	a:=0
	while(a<strsplit(note).length()){
		++a

		if(RegExMatch(note, "\d+", var,a)>a)
			a:=RegExMatch(note, "\d+ USD",var,a)
		if(strsplit(var).length()>4)                                      
			++a
		if(var)
			vars[vars.length()+1]:=strsplit(var," ")[1]
	}
	
	for a, v in vars
		msgbox, %v%

return
carno wrote:Hi, magicinmath, thanks for the effort, but this fails when I paste my wall of text. There are many other numerals before and after the sentence I highlighted in red (see above). I am seeking just the number (no USD) in that particular sentence I highlighted in red that is somewhere in the wall of text.
Edit * please see with object vars holding each number preceding USD.
If its not working with a particular block of text please provide that example.
In the block of text I've included within %note% I've added various numbers and several occurences of the amount to find.
I've tested with between 1 and 6 digits preceding USD.
Last edited by magicinmath on 11 Oct 2017, 16:49, edited 4 times in total.
carno
Posts: 265
Joined: 20 Jun 2014, 16:48

Re: RegEx to find variable dollar amount in a wall of text

11 Oct 2017, 16:11

Hi, magicinmath, thanks for the effort, but this fails when I paste my wall of text. There are many other numerals before and after the sentence I highlighted in red (see above). I am seeking just the number (no USD) in that particular sentence I highlighted in red that is somewhere in the wall of text.

PS: I added "StringTrimRight, v, v, 4" and now it works fine. (I had removed USD in "a:=RegExMatch(note, "\d+ USD",var,a)" and that is why it was failing.)

Code: Select all

vars:=[]

	a:=0
	while(a<strsplit(note).length()){
		++a

		if(RegExMatch(note, "\d+ USD", var,a)>a)
			a:=RegExMatch(note, "\d+ USD",var,a)
		if(strsplit(var).length()>4)                                      
			++a
		if(var)
			vars[vars.length()+1]:=var
	}

	for a, v in vars
	StringTrimRight, v, v, 4
		msgbox, %v%

return
carno
Posts: 265
Joined: 20 Jun 2014, 16:48

Re: RegEx to find variable dollar amount in a wall of text

11 Oct 2017, 20:03

magicinmath wrote:I'm just learning RegExMatch but this is what I figured out so far. Probably a more efficient way yet hopefully someone can school us both!

Code: Select all

note=
(
...
stuff here blah blahblhablhablah sklchsldkn klcnaslnk 2345 34 5 333
Based on past jobs we think we can get this job done for 5 USD (excl. tax) but we are not set on this as we also want ...
Based on past jobs we think we can get this job done for 20 USD (excl. tax) but we are not set on this as we also want ...
textBased on past jobs we think we can get this job done for 75 USD (excl. tax) but we are not set on this as we also want ...
textBased on past jobs we think we can get this job done for 500 USD (excl. tax) but we are not set on this as we also want ...
text
text
...
)

vars:=[]

	a:=0
	while(a<strsplit(note).length()){
		++a

		if(RegExMatch(note, "\d+", var,a)>a)
			a:=RegExMatch(note, "\d+ USD",var,a)
		if(strsplit(var).length()>4)                                      
			++a
		if(var)
			vars[vars.length()+1]:=strsplit(var," ")[1]
	}
	
	for a, v in vars
		msgbox, %v%

return
carno wrote:Hi, magicinmath, thanks for the effort, but this fails when I paste my wall of text. There are many other numerals before and after the sentence I highlighted in red (see above). I am seeking just the number (no USD) in that particular sentence I highlighted in red that is somewhere in the wall of text.
Edit * please see with object vars holding each number preceding USD.
If its not working with a particular block of text please provide that example.
In the block of text I've included within %note% I've added various numbers and several occurences of the amount to find.
I've tested with between 1 and 6 digits preceding USD.
Hi, magicinmath, thanks again, I had removed USD from "a:=RegExMatch(note, "\d+ USD",var,a)" and that is why it was failing. But now I analyzed you code in more detail and realized my error. Your code is working fine now and I appreciate your great work! This is the version of your code that works for me now ( :) ):

Code: Select all

vars:=[]

	a:=0
	while(a<strsplit(note).length()){
		++a

		if(RegExMatch(note, "\d+ USD", var,a)>a)
			a:=RegExMatch(note, "\d+ USD",var,a)
		if(strsplit(var).length()>4)                                      
			++a
		if(var)
			vars[vars.length()+1]:=strsplit(var," ")[1]
	}
	
	for a, v in vars
		msgbox, %v%

return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Insanibaccha, PsysimSV and 203 guests