Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract text matching a regex using Vim?

I would like to extract some data from a text with vim.. the data is of this kind:

72" title="(168,72)" onmouseover="posizione('(168,72)');" onmouseout="posizione('(-,-)');">>
72" title="(180,72)" onmouseover="posizione('(180,72)');" onmouseout="posizione('(-,-)');">>
72" title="(192,72)" onmouseover="posizione('(192,72)');" onmouseout="posizione('(-,-)');">>
72" title="(204,72)" onmouseover="posizione('(204,72)');" onmouseout="posizione('(-,-)');">>

The data I need to extract is contained in: title="(168,72)".
In particular I am interested in extracting only these coordinates.

I though about using vim to first delete everything before title=" .. but I am not really a guru of regex.. so I am asking you: if anyone has any hint: please tell me :)

like image 789
nick2k3 Avatar asked Jul 03 '11 18:07

nick2k3


4 Answers

This will replace each line with a tab-delimited list of coordinates per line:

:%s/.* title="(\(\d\+\),\(\d\+\))".*/\1\t\2
like image 69
MrWednesday Avatar answered Nov 08 '22 10:11

MrWednesday


This task can be achieved with a much simpler solution and with few keystrokes using normal command:

:%normal df(f)D

This means:

  1. % - Run normal command on all file lines;
  2. normal - run the following commands in normal mode;
  3. df( - delete everything until you find a parenthesis (parenthesis included);
  4. f) - move the cursor to );
  5. D - delete everything until the end of the line.

You can also set a range, for example, run this from line 5 to 10:

:5,10normal df(f)D
like image 31
Magnun Leno Avatar answered Nov 08 '22 11:11

Magnun Leno


If you want an ad hoc solution for this one-off case, it might be quicker simply to select a visual block using CTRL-v. This will let you select an arbitrary column of text (in your case, the column containing title="(X,Y)"), which can then be copied as usual using y.

like image 3
Prince Goulash Avatar answered Nov 08 '22 10:11

Prince Goulash


you can match everything inside title=() and discard everything else like this:

:%s,.*title="(\(.*\))".*,\1,
like image 1
Ярослав Рахматуллин Avatar answered Nov 08 '22 09:11

Ярослав Рахматуллин