tags:

views:

102

answers:

5
+1  Q: 

Little vim regex

I have a bunch of strings that look like this: '../DisplayPhotod6f6.jpg?t=before&tn=1&id=130', and I'd like to take out everything after the question mark, to look like '../DisplayPhotod6f6.jpg'.

s/\(.\.\.\/DisplayPhoto.\{4,}\.jpg\)*'/\1'/g

This regex is capturing some but not all occurences, can you see why?

+4  A: 

\.\{4,} is trying to match 4 or more . characters. What it looks like you wanted is "match 4 or more of any character" (.\{4,}) but "match 4 or more non-. characters" ([^.]\{4,}) might be more accurate. You'll also need to change the lone * at the end of the pattern to .* since the * is currently applying to the entire \(\) group.

jamessan
oh duh.. Thanks!
nnyby
nnyby
+1  A: 

The following regexp: /(\.\./DisplayPhoto.*\.jpg)/gi

tested against following examples:

../DisplayPhotocef3.jpg?t=before&tn=1&id=54
../DisplayPhotod6f6.jpg?t=before&tn=1&id=130

will result:

../DisplayPhotocef3.jpg
../DisplayPhotod6f6.jpg
jerone
That's PCRE, which is different from Vim's regex.
jamessan
+3  A: 

I think the easyest way to go for this is:

s/?.*$/'/g

This says: delete everything after the question mark and replace it with a single quote.

matias
A: 
%s/\('\.\.\/DisplayPhoto\w\{4,}\.jpg\).*'/\1'/g

Some notes:

  • % will cause the swap to work on all lines.
  • \w instead of '.', in case there are some malformed file names.
  • Replace '.' at the start of your matching regex with ' which is exactly what it should be matching.
Swiss
+1  A: 

I would use macros, sometime simpler than regexp (and interactive) :

qa
/DisplayPhoto<Enter>
f?dt'
n
q

And then some @a, or 20000@a to go though all lines.

Drasill