0

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
2
  • 1
    Don't forget that if you want to test out Regular Expressions in Emacs, you can use M-x regexp-builder Commented Dec 21, 2013 at 9:59
  • To add to the great answer by @Andreas Röhler, Emacs might or might not be the right tool for this, depending on the use case. How will this fit into something bigger? Does a command-line tool make more sense? Commented Dec 22, 2013 at 2:10

2 Answers 2

1

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 .+?'\(.+?\)'.+?'\(.+?\)'.+.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.