incandescentman
incandescentman

Reputation: 6378

how do I duplicate a regular expression

I want to search for all the phrases that are in quotes and then duplicate them.

In Emacs, my search looks like this:

M-x replace-regexp <RETURN> "*" <RETURN> $& $& <RETURN>

But it doesn't work...

Upvotes: 1

Views: 92

Answers (1)

Mark Reed
Mark Reed

Reputation: 95315

"*" doesn't match a quote-delimited string; it matches any (nonzero) number of quotation marks in a row.

In regular expressions, * is a modifier, not the wildcard that it is in filename patterns. It means "match 0 or more of the previous pattern", so "*" is "0 or more quotation marks, followed by a quotation mark" - that is, any number of quotation marks in a row.

The usual regular expression for "anything" is .* which matches 0 or more of "any character" (.). But that would include quotation marks; ".*" would match everything from the first quotation mark to the last as one big string. What you want is "[^"]*", which matches a quotation mark, followed by any number of non-quotation marks, followed by another quotation mark.

Also, the matched pattern is \&, not $&. This might work better:

M-x replace-regexp <RETURN> "[^"]*" <RETURN> \& \& <RETURN>

Upvotes: 5

Related Questions