Posts: 97
Threads: 48
Joined: Sep 2010
Hi there,
I have a amount of text on the clipboard, from which i need several parts. Each part starts with "start" and ends with 'contract''.
What i like is that the macro searches the content en stores every part. So that the parts are available for later usage.
Can this be done?
Greetings Sonic
Posts: 12,098
Threads: 142
Joined: Dec 2002
Macro
Macro471
;set clipboard text for testing
str s1=
;...
;... start
;A contract ...
;...
;... start B contract ...
;...
;... start C contract ...
s1.setclip
;--------------
;macro
str s.getclip
if(s.len=0) ret
ARRAY(str) a; int i
findrx(s "(?s)\bstart\b.+?\bcontract\b" 0 4 a)
;remove (?s) if text between start and contract cannot be multiline
;results
for i 0 a.len
,out a[0 i]
Posts: 97
Threads: 48
Joined: Sep 2010
Hi,
I tested your code, and it works but only if the result is in one line. I changed the flags to 4|8 and nothing changed...
So when the clipboard contains text like this:
;...
;... start
A contract ...
;...
;... start B contract ...
;...
;... start C contract ...
the first result is not found..
Any ideas to solve this?
Grz
Sonic
Posts: 12,098
Threads: 142
Joined: Dec 2002
sorry, the comment about flag 8 was incorrect. Need (?s). See the updated version.
Posts: 97
Threads: 48
Joined: Sep 2010
Thx Gintaras,
Thank you for your quick reply!
1 more question if i may...
If the clipboard had a content like this: ;...
;... start
;A ...
;...
;... start B
contract ...
;...
;... start C contract ...
how do i get the result of the search without start A.. so only the last 'start' and 'contract' (B) should be in the result.
Can you help me with this?
Posts: 12,098
Threads: 142
Joined: Dec 2002
Add .+ at the beginning of the regular expression. It skips as much as possible, until the last match.
Macro
Macro490
;set clipboard text for testing
str s1=
;...
;... start
;A contract ...
;...
;... start B contract ...
;...
;... start C contract ...
s1.setclip
;--------------
;macro
str s.getclip
if(s.len=0) ret
str a; int i
findrx(s "(?s)^.+\b(start\b.+?\bcontract)\b" 0 0 a 1)
;remove (?s) if text between start and contract cannot be multiline
;results
out a
Posts: 97
Threads: 48
Joined: Sep 2010
Thanks again for your time...
I think i didn't explain it right. My example of the clipboard's contents in the last post was changed. I meant that only the last 'start' found before 'contract' should be in te result. Every one of them. Now i get only the last result.
Posts: 12,098
Threads: 142
Joined: Dec 2002
With regular expression I don't know how, but can remove the first "start..." later.
Macro
Macro471
;set clipboard text for testing
str s1=
;... start
;A ...
;...
;... start B
;contract ...
;...
;... start C contract ...
s1.setclip
;--------------
;macro
str s.getclip
if(s.len=0) ret
ARRAY(str) a; int i
findrx(s "(?s)\bstart\b.+?\bcontract\b" 0 4 a)
;remove (?s) if text between start and contract cannot be multiline
;results
for i 0 a.len
,str& r=a[0 i]
,int j=FindLast(r "start" 2)
,if(j>0) r.remove(0 j)
,out r