Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

It is clear to me how to programmatically replace all instances of regex in a buffer with some text, something like (pardon my naive code):

(defun my/regex-global-replace (regex subst-text)
 "Globally replace `regex' with `subst-text' in current buffer"
 (goto-char (point-min))
 (while (re-search-forward regex nil t)
 (replace-match subst-text)))

But I can't figure it out how to do a similar global replace when using capture groups and sub-matches.

I am aware of this answerthis answer and the Cookbook entry, but it does not help me much, since it is not clear how I could use the sub-matches to replace text globally.

How could one write a broader version of the code above, so that it deals with sub-matches, something like:

(defun my/regex-global-replace (regex1 regex2)
 "Replace `regex1' with `regex2' in a buffer."
 (goto-char (point-min))
 (while (re-search-forward regex1 nil t)
 (replace-match regex2)))

Where regex1 may contain a certain number of capture groups, like,

(setq regex1 "some text \\([[:digit:]]+\\)")

and regex2 a related sub-match, the equivalent of PCRE's $1 or \1?


Example

As an example, say I have a buffer with content,

Some text 123
Some more text 456

I would like to programmatically run a global search-and-replace that would, as example, change text but keep the numbers,

Changed text, keep digits 123
Changed text, keep digits 456

In some other languages (Perl comes to mind), I would create a search regex with a capture group, and a replace expression using the sub-matches, something like

s/(\w+\s+)+(\d+)\n/Changed text, keep digits \2\n/gim

It is clear to me how to programmatically replace all instances of regex in a buffer with some text, something like (pardon my naive code):

(defun my/regex-global-replace (regex subst-text)
 "Globally replace `regex' with `subst-text' in current buffer"
 (goto-char (point-min))
 (while (re-search-forward regex nil t)
 (replace-match subst-text)))

But I can't figure it out how to do a similar global replace when using capture groups and sub-matches.

I am aware of this answer and the Cookbook entry, but it does not help me much, since it is not clear how I could use the sub-matches to replace text globally.

How could one write a broader version of the code above, so that it deals with sub-matches, something like:

(defun my/regex-global-replace (regex1 regex2)
 "Replace `regex1' with `regex2' in a buffer."
 (goto-char (point-min))
 (while (re-search-forward regex1 nil t)
 (replace-match regex2)))

Where regex1 may contain a certain number of capture groups, like,

(setq regex1 "some text \\([[:digit:]]+\\)")

and regex2 a related sub-match, the equivalent of PCRE's $1 or \1?


Example

As an example, say I have a buffer with content,

Some text 123
Some more text 456

I would like to programmatically run a global search-and-replace that would, as example, change text but keep the numbers,

Changed text, keep digits 123
Changed text, keep digits 456

In some other languages (Perl comes to mind), I would create a search regex with a capture group, and a replace expression using the sub-matches, something like

s/(\w+\s+)+(\d+)\n/Changed text, keep digits \2\n/gim

It is clear to me how to programmatically replace all instances of regex in a buffer with some text, something like (pardon my naive code):

(defun my/regex-global-replace (regex subst-text)
 "Globally replace `regex' with `subst-text' in current buffer"
 (goto-char (point-min))
 (while (re-search-forward regex nil t)
 (replace-match subst-text)))

But I can't figure it out how to do a similar global replace when using capture groups and sub-matches.

I am aware of this answer and the Cookbook entry, but it does not help me much, since it is not clear how I could use the sub-matches to replace text globally.

How could one write a broader version of the code above, so that it deals with sub-matches, something like:

(defun my/regex-global-replace (regex1 regex2)
 "Replace `regex1' with `regex2' in a buffer."
 (goto-char (point-min))
 (while (re-search-forward regex1 nil t)
 (replace-match regex2)))

Where regex1 may contain a certain number of capture groups, like,

(setq regex1 "some text \\([[:digit:]]+\\)")

and regex2 a related sub-match, the equivalent of PCRE's $1 or \1?


Example

As an example, say I have a buffer with content,

Some text 123
Some more text 456

I would like to programmatically run a global search-and-replace that would, as example, change text but keep the numbers,

Changed text, keep digits 123
Changed text, keep digits 456

In some other languages (Perl comes to mind), I would create a search regex with a capture group, and a replace expression using the sub-matches, something like

s/(\w+\s+)+(\d+)\n/Changed text, keep digits \2\n/gim
edited tags
Link
Drew
  • 80.9k
  • 10
  • 125
  • 265
Clarify example
Source Link
gsl
  • 1.9k
  • 19
  • 36

It is clear to me how to programmatically replace all instances of regex in a buffer with some text, something like (pardon my naive code):

(defun my/regex-global-replace (regex subst-text)
 "Globally replace `regex' with `subst-text' in current buffer"
 (goto-char (point-min))
 (while (re-search-forward regex nil t)
 (replace-match subst-text)))

But I can't figure it out how to do a similar global replace when using capture groups and sub-matches.

I am aware of this answer and the Cookbook entry, but it does not help me much, since it is not clear how I could use the sub-matches to replace text globally.

How could one write a broader version of the code above, so that it deals with sub-matches, something like:

(defun my/regex-global-replace (regex1 regex2)
 "Replace `regex1' with `regex2' in a buffer."
 (goto-char (point-min))
 (while (re-search-forward regex1 nil t)
 (replace-match regex2)))

Where regex1 may contain a certain number of capture groups, like,

(setq regex1 "some text \\([[:digit:]]+\\)")

and regex2 a related sub-match, the equivalent of PCRE's $1 or \1?


Example

As an example, say I have a buffer with content,

Some text 123
Some more text 456

I would like to programmatically run a global search-and-replace that would, as example, onlychange text but keep the numbers,

Changed text, keep digits 123
Changed text, keep digits 456

In some other languages (Perl comes to mind), I would create a search regex with a capture group, and a replace expression using the sub-matches, something like

s/(\w+\s*\w+\s+)+(\d+)\n/\2Changed text, keep digits \2\n/gim

It is clear to me how to programmatically replace all instances of regex in a buffer with some text, something like (pardon my naive code):

(defun my/regex-global-replace (regex subst-text)
 "Globally replace `regex' with `subst-text' in current buffer"
 (goto-char (point-min))
 (while (re-search-forward regex nil t)
 (replace-match subst-text)))

But I can't figure it out how to do a similar global replace when using capture groups and sub-matches.

I am aware of this answer and the Cookbook entry, but it does not help me much, since it is not clear how I could use the sub-matches to replace text globally.

How could one write a broader version of the code above, so that it deals with sub-matches, something like:

(defun my/regex-global-replace (regex1 regex2)
 "Replace `regex1' with `regex2' in a buffer."
 (goto-char (point-min))
 (while (re-search-forward regex1 nil t)
 (replace-match regex2)))

Where regex1 may contain a certain number of capture groups, like,

(setq regex1 "some text \\([[:digit:]]+\\)")

and regex2 a related sub-match, the equivalent of PCRE's $1 or \1?


Example

As an example, say I have a buffer with content,

Some text 123
Some more text 456

I would like to programmatically run a global search-and-replace that would, as example, only keep the numbers,

123
456

In some other languages (Perl comes to mind), I would create a search regex with a capture group, and a replace expression using the sub-matches, something like

s/(\w+\s*)+(\d+)/\2/gim

It is clear to me how to programmatically replace all instances of regex in a buffer with some text, something like (pardon my naive code):

(defun my/regex-global-replace (regex subst-text)
 "Globally replace `regex' with `subst-text' in current buffer"
 (goto-char (point-min))
 (while (re-search-forward regex nil t)
 (replace-match subst-text)))

But I can't figure it out how to do a similar global replace when using capture groups and sub-matches.

I am aware of this answer and the Cookbook entry, but it does not help me much, since it is not clear how I could use the sub-matches to replace text globally.

How could one write a broader version of the code above, so that it deals with sub-matches, something like:

(defun my/regex-global-replace (regex1 regex2)
 "Replace `regex1' with `regex2' in a buffer."
 (goto-char (point-min))
 (while (re-search-forward regex1 nil t)
 (replace-match regex2)))

Where regex1 may contain a certain number of capture groups, like,

(setq regex1 "some text \\([[:digit:]]+\\)")

and regex2 a related sub-match, the equivalent of PCRE's $1 or \1?


Example

As an example, say I have a buffer with content,

Some text 123
Some more text 456

I would like to programmatically run a global search-and-replace that would, as example, change text but keep the numbers,

Changed text, keep digits 123
Changed text, keep digits 456

In some other languages (Perl comes to mind), I would create a search regex with a capture group, and a replace expression using the sub-matches, something like

s/(\w+\s+)+(\d+)\n/Changed text, keep digits \2\n/gim
Add example
Source Link
gsl
  • 1.9k
  • 19
  • 36
Loading
Add details
Source Link
gsl
  • 1.9k
  • 19
  • 36
Loading
Clarify title
Source Link
gsl
  • 1.9k
  • 19
  • 36
Loading
Source Link
gsl
  • 1.9k
  • 19
  • 36
Loading