How to extract text matching a regex using Vim?
Asked Answered
B

4

7

I would like to extract some data from a piece of text with Vim. The input looks like so:

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 the title="(168,72)" portions of the input. In particular, I am interested in extracting coordinate pairs in parentheses.

I thought about using Vim to first delete everything before title=", but I am not really a regex guru, so I am asking you. If anyone has any hint, please let me know! :)

Brimstone answered 3/7, 2011 at 18:44 Comment(1)
Like other posted vim use of stream expression is very powerful. These stream manipulator are also available whit sed (almost the same thing whitout the :%). If you want to learn more about these expressions have a look at this sed introduction.Acclivity
H
5

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

:%s/.* title="(\(\d\+\),\(\d\+\))".*/\1\t\2
Hesperus answered 3/7, 2011 at 18:50 Comment(0)
Z
4

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
Zinnes answered 4/7, 2011 at 17:19 Comment(2)
Interesting way to do this without macrosBandoleer
normal command is a very powerful tool. It's almost a "one time macro".Zinnes
A
3

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.

Arthur answered 3/7, 2011 at 19:31 Comment(1)
Yeah a quick no-brain series I find my self using is: ^vGx....... judging the number of repeats (.) visually - pragmatic but works fine 80% of the casesUnblinking
K
1

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

:%s,.*title="(\(.*\))".*,\1,
Kan answered 3/7, 2011 at 18:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.