Bug 94837 - grouping and back-references in regular expressions
Summary: grouping and back-references in regular expressions
Status: RESOLVED FIXED
Alias: None
Product: klipper
Classification: Applications
Component: general (show other bugs)
Version: unspecified
Platform: unspecified Linux
: NOR wishlist
Target Milestone: ---
Assignee: Esben Mose Hansen
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-12-10 19:58 UTC by Christoph Lange
Modified: 2008-05-30 21:15 UTC (History)
1 user (show)

See Also:
Latest Commit:
Version Fixed In:


Attachments
grouping support (3.60 KB, patch)
2007-06-07 21:42 UTC, sean finney
Details
klipper-grouping.patch (3.43 KB, text/x-diff)
2007-06-09 13:51 UTC, sean finney
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Christoph Lange 2004-12-10 19:58:01 UTC
Version:           v0.9.6 (using KDE 3.3.1, Gentoo)
Compiler:          gcc version 3.3.4 20040623 (Gentoo Linux 3.3.4-r1, ssp-3.3.2-2, pie-8.7.6)
OS:                Linux (i686) release 2.6.9-gentoo-r6

I'd like to use grouping with (...) in the regular expressions which fire actions and to be able to refer to these matches using $1 or \1 or whatever. %s for the whole match is insufficient for advanced action triggers.

See for example my approach to open wiki links like [[Wiki]] in the Wikipedia:

regexp: \[\[[^]]+\]\]
action: kfmclient exec "http://de.wikipedia.org/w/wiki.phtml?search=`echo %s | sed 's/^\[\[\|\]\]$//g' | recode l1..u8`&go=Go"

IMHO it's too much overhead to call external programs like sed in this case. I'd like to do it this way:

regexp: \[\[([^]]+)\]\]
action: kfmclient exec http://de.wikipedia.org/w/wiki.phtml?search=\1&go=Go
Comment 1 Esben Mose Hansen 2004-12-10 20:13:29 UTC
Well, Klipper currently uses QT's regular expression library, which supports what you want:

http://doc.trolltech.com/3.3/qregexp.html#capturedTexts

This should be a simple task for anyone who wants to do it; if noone else does I will get around to it :) 

Comment 2 sean finney 2007-06-07 21:41:28 UTC
hi there,

i have a patch for this, attached.  

so now, in addition to '%s', you also have %n, where n is a non-negative 
integer.  %0 is equivalent to %s, %1 matches the first group, etc.  values of 
n greater than the largest group index don't get expanded.  my reason for choosing %n instead of \n or $n is that this way i could use the kmacro expansion stuff out of the box (so no ugly string mangling or anything).

the patch is fairly minimal, and i did a cursory valgrind check to make sure 
that it wasn't leaking memory (though someone more familiar with QStringList 
might want to double check my logic).


   sean
Comment 3 sean finney 2007-06-07 21:42:11 UTC
Created attachment 20802 [details]
grouping support

see previous comments.
Comment 4 sean finney 2007-06-07 21:44:32 UTC
oh btw, you might notice i removed myCurrentAction... it wasn't being used at all anyway (and sent me down the wrong path for a bit :p )
Comment 5 Esben Mose Hansen 2007-06-09 10:26:19 UTC
I'll look at the patch today. Thank you for making it :)
Comment 6 Esben Mose Hansen 2007-06-09 11:16:42 UTC
Sorry, I didn't notice this before, but this patch is against the 3.5.x series branch. I'm not sure if new features are accepted against this branch at all, but in any case, I don't feel I can apply the patch on the branch. Any chance of a patch against trunk? I tried to apply it, but it looks like it would need a bit of work to be ported. 
Comment 7 sean finney 2007-06-09 11:53:01 UTC
hi,

On Saturday 09 June 2007 11:16:43 Esben Mose Hansen wrote:
> ------- Sorry, I didn't notice this before, but this patch is against the
> 3.5.x series branch. I'm not sure if new features are accepted against this
> branch at all, but in any case, I don't feel I can apply the patch on the
> branch. Any chance of a patch against trunk? I tried to apply it, but it
> looks like it would need a bit of work to be ported.


well.. that's what i got to work with.   it looks like the layout is different 
in the latest svn than the kdebase debian source package.  where do i find 
klipper's source in trunk?


	sean
Comment 8 Esben Mose Hansen 2007-06-09 13:12:46 UTC
Klipper has moved one directory in, under workspace

svn+ssh://esben@svn.kde.org/home/kde/trunk/KDE/kdebase/workspace/klipper
Comment 9 sean finney 2007-06-09 13:51:33 UTC
> ------- Additional Comments From kde mosehansen dk  2007-06-09 13:12
> ------- Klipper has moved one directory in, under workspace
>
> svn+ssh://esben svn kde org/home/kde/trunk/KDE/kdebase/workspace/klipper


cool, used anonsvn to get at it from there.  attached is an updated patch 
which i think should work against trunk.  most of the changes were noise from 
nearby changed code.  so my added code is still the same, minus a typechange 
from QIntDict to Q3IntDict somewhere, since i was following the lead of the
myCommandMapping, which also had this change.

i haven't tried compiling it, but don't imagine that there should be any 
significant problems.

also, this new patch doesn't contain the removal of myCurrentAction from the 
affected files, but grep shows this variable to still be there and unused, so 
you might want to take it out while you're at it.


	sean


Created an attachment (id=20816)
klipper-grouping.patch
Comment 10 Pino Toscano 2008-05-30 21:15:12 UTC
SVN commit 814585 by pino:

add a parent of the command items, and a way to get the captured matches from the action items
this way, we can replace up to 9 submatches in the regexp of an action in the resulting command (and %0) too
FEATURE: 94837


 M  +1 -1      configdialog.cpp  
 M  +18 -3     urlgrabber.cpp  
 M  +3 -1      urlgrabber.h  


WebSVN link: http://websvn.kde.org/?view=rev&revision=814585