Mingo
Mingo

Reputation: 1613

emacs regular expression how to get specific columns

I have the following text file:

Input text file:

<Person FirstName='Steve' LastName='Smith' Phone='555-12345' Title='Mr.' BirthDate='1950-01-01' />

<Person FirstName='Lin' LastName='Dan' Phone='555-12345' Title='Mr.' BirthDate='1950-01-01' />

I want to get the following:

expect output text:

Steve Smith
Lin Dan

Upvotes: 0

Views: 95

Answers (2)

Andreas R&#246;hler
Andreas R&#246;hler

Reputation: 4804

Giving another answer still, being as specific as possible makes regular expressions more reliable. You want the contents of precise slots, so let's use its names:

(defun my-name-slots ()
  (interactive)
  (while (re-search-forward "FirstName='\\([^']+\\)'[ \t]+LastName='\\([^']+\\)'" nil t 1)
    (message "%s %s" (match-string-no-properties 1) (match-string-no-properties 2) )))

As (match-string-no-properties 1) (match-string-no-properties 2) holding the output looked for,

(list ...)

at the end of the function would return it as a list.

Upvotes: 1

itsjeyd
itsjeyd

Reputation: 5280

You can use a regexp with back references for this:

M-x query-replace-regexp

RET .+?'\([A-Za-z]+\)'.+?'\([A-Za-z]+\)'.+

RET \1 \2

The trick is to write a regexp that matches the whole line, while putting the content that you want to keep into parenthesized groups that you can reference in the output you want to generate.

See also this answer to a similar question for a bit more info about back references and a link to relevant portions of the Emacs manual.


EDIT: A shorter version of the regexp to replace that also works for the example input you gave would be .+?'\(.+?\)'.+?'\(.+?\)'.+.

Upvotes: 1

Related Questions