Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extracting text from clipboard
#1
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
#2
Macro Macro471
Code:
Copy      Help
;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]
#3
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
#4
sorry, the comment about flag 8 was incorrect. Need (?s). See the updated version.
#5
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?
#6
Add .+ at the beginning of the regular expression. It skips as much as possible, until the last match.

Macro Macro490
Code:
Copy      Help
;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
#7
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.
#8
With regular expression I don't know how, but can remove the first "start..." later.

Macro Macro471
Code:
Copy      Help
;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


Forum Jump:


Users browsing this thread: 1 Guest(s)