copy regex captures to the clipboard

Find, replace, find in files, replace in files, regular expressions

copy regex captures to the clipboard

Postby Wow_Zow » Tue Jan 30, 2007 7:47 pm

hey group --

First time poster. New UE user. Go easy on me. :lol:

I'm looking to do a pretty simple task with a macro but can't quite work it out after an hour and a half of mucking about. I have a file as follows:

Code: Select all
 34.a UnitTest12: xxy
 37.b  blah blahUnitTest:  yzz
 34.a UnitTest009: zyz
 42.c  yadda, UnitTestadda: xyz
 34.a UnitTest74: xxx

I'd like to capture every occurance of "UnitTest\d.*" that has "34.a" in front of it, and append that to the clipboard string. The final result on the clipboard should look like this:
---------> UnitTest12, UnitTest009, UnitTest74

The regex to grab the captures is not a problem, what I have is working fine In UE. (I'm using Perl style, NOT Unix or UE, here):
---------> \s34.a\s*(UnitTest\d.*)\s*[:]

Where Im totally lost with UE is: How to append all the captures ($1) to the clipboard in one long string using a macro.

Anyone have a macro snippet? Im probably approaching it all wrong by not looping or something.

Tnx
WZ
User avatar
Wow_Zow
Basic User
Basic User
 
Posts: 11
Joined: Tue Jan 30, 2007 12:00 am

Re: copy regex captures to the clipboard

Postby Wow_Zow » Tue Jan 30, 2007 9:45 pm

Hmmmm.....

...I've also been trying to use a Perl Positive Lookbehind to capture exactly what I need directly w/o using a capture submatch I originally posted.

Instead of using this perl and trying to use the $1 capture in a macro:
------> \s34.a\s*(UnitTest\d.*)\s*[:]

I thought I could do a "find" using this regex,(works great in Perl):
------> (?<=34.a\s*)U\w*

But that doesn't work in UE v12.20 either. It returns: "You have entered an invalid expression". Doh.

So Im still stuck without a regex I can use in the "find" macro command. hrmpffff. I'm obviously missing something very elementary here.

wz
User avatar
Wow_Zow
Basic User
Basic User
 
Posts: 11
Joined: Tue Jan 30, 2007 12:00 am

Re: copy regex captures to the clipboard

Postby Bego » Wed Jan 31, 2007 8:04 am

Hi Wow,

well, a lookbehind ONLY works for plain text, if I understood it right.

E.g. look here for "lookbehind": http://www.regular-expressions.info/refadv.html
Since regular expressions cannot be applied backwards, the test inside the lookbehind can only be plain text.

So I think you have to use your loop and a copyappend in your macro.

rds bego
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: copy regex captures to the clipboard

Postby Mofi » Wed Jan 31, 2007 9:36 am

Use following macro with the macro property Continue if a Find with Replace not found checked for this macro.

InsertMode
ColumnModeOff
HexOff
UnixReOn
ClearClipboard
Loop
Find RegExp "\s*34\.a\s*UnitTest\d+"
IfFound
Key HOME
Find RegExp "UnitTest\d+"
CopyAppend
EndSelect
Key LEFT ARROW
Key RIGHT ARROW
","
StartSelect
Key LEFT ARROW
CutAppend
EndSelect
Else
ExitLoop
EndIf
EndLoop

Add UnixReOff or PerlReOn (v12+ of UE) at the end of the macro if you do not use Unix style regular expressions by default - see search configuration. Macro command UnixReOn sets the regular expression option to Unix style.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4064
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: copy regex captures to the clipboard

Postby Bego » Wed Jan 31, 2007 9:47 am

Hi Mofi,

IfFound
Key HOME
Find RegExp "UnitTest\d+"

that one is clever. I tried to "extract" the $1 anyhow, but a simple goto start of line and do another search only kicks it.

n1, bego
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: copy regex captures to the clipboard

Postby Wow_Zow » Wed Jan 31, 2007 12:55 pm

Bego wrote:Well, a lookbehind ONLY works for plain text, if I understood it right.

I think you're right, Bego. Once I used a literal string inside the lookback it was fine in UE.

I have a tool called "RegexBuddy" that I used to create the incorrect lookback regex. Unfortunately, it DOES allow character pattern matches inside the lookback even though it shouldn't when set to run in perl.

(BTW, RB is great inexpensive tool for learning regex even though it has a goofy azz name. You can set it to work for various flavors of regex such as perl, .net, 'nix, vbscript, etc.).

Thanks for the heads up.

Wz
User avatar
Wow_Zow
Basic User
Basic User
 
Posts: 11
Joined: Tue Jan 30, 2007 12:00 am

Re: copy regex captures to the clipboard

Postby Wow_Zow » Wed Jan 31, 2007 1:12 pm

Thanks Mofi.

Good idea to backtrack and make your own "subselection capture". :lol:

Also, good catch escaping my literal '.' with '\.' That's definitely what I should have used.

-----------------

Wow_Zow wrote:Thanks for all your help {Mofi} --- and thanks for being such a prolific poster. You saved me quite a bit of work with your hints and tips, as I read through the your regex and macro forum responses.

tnx mate,
Wz


From a thread over in 'macros' that you helped with too.

Cheers mate!
Wz
User avatar
Wow_Zow
Basic User
Basic User
 
Posts: 11
Joined: Tue Jan 30, 2007 12:00 am


Return to Find/Replace/Regular Expressions