Questions tagged [regular-expressions]

is for patterns specifying search or replace strings. Also known as `regex` or `regexp`, the patterns describe strings to match when searching or replacing. Emacs provides extensive support for `regex` patterns in many commands. Emacs also provides an interactive expression builder for such patterns.

Filter by
Sorted by
Tagged with
8
votes
3answers
575 views

Search and replace through a multifile project in AUCTeX?

I am about to adopt Emacs to be the only editor I use in all what I am writing including LaTeX documents. I came from WYSIWYG editor which served that purpose fairly nice, namely TexStudio or TXS for ...
0
votes
1answer
836 views

Regex to check for a markdown link at point

I am trying to develop a regex to check if points is within a GitHub markdown link to a wiki page, and then extract that page name. For example some text [link_name](https://github.com/user/wiki/Tool-...
3
votes
1answer
64 views

Emacs regex replace while re-using part of the expression in the replacement

I sometimes need to do replacements which would involve using matched parts for the output. For example, I would like to replace varName->doWork() with ::doWork(varName, 42) whatever varName ...
2
votes
3answers
3k views

Regular expression to match text within quotes

I have a text like this: ''Lorem ipsum'' dolor ''sit amet, '' consectetur adipiscing elit. What I want is to get the sit amet correctly wrapped around with '', like this: ''Lorem ipsum'' dolor ''...
1
vote
1answer
2k views

Matching any printable character but whitespace

I would like to match a word (that can contain any printable characters except space) separated on the left side by whitespace and on the right side by a newline or whitespace. What is the ...
26
votes
1answer
4k views

Why do regular expressions created with the regex builder use syntax different from the interactive regular expressions?

So, using the regular expression builder (M-x re-builder), finding lines that end in \ takes "\\$", while in search and replace by regex, it only takes "\$". I would have expected the regex builder ...
3
votes
2answers
242 views

How to clear regexp-replace history?

I wrote a bad bit of elisp when trying to do a query-replace-regexp. The text to replace was like this: * list item And I wanted it to be * List item My query-replace-regexp match was written as: ...
3
votes
1answer
67 views

Match Perl identifier using re-search

I am trying to match a Perl identifier at the point, like My::Module::dummy_func. I'd like to start simple and require a valid identifier as follows: restricted to a single line starts with a space ...
5
votes
1answer
928 views

How to save part of a regular expression during search and replace?

I think the best way to explain what I'm trying to do is using an example. Suppose one would want to replace all underlined words with italic ones in org-mode using a regular expression: _example_ --&...
6
votes
1answer
340 views

Using the count of coincidence from a regexp as replacement text?

I have an org-mode file with more than 12000 footnotes (maybe less), I was cleaning it and found that after the conversion some footnotes weren't converted to org-mode footnotes, so I had to manually ...
5
votes
2answers
228 views

Replace a two-line string not ending in digit with a one line string

I would like to replace a two-line string not ending in digit with a one line string. For example, replacing the following two-line string: Extensions of the Single—Sa1e Model 414 With a one line ...
4
votes
1answer
182 views

Regexp to parse Hy errors for Flycheck

I'm trying to write a Flycheck checker definition based on Hy, using the API here. As part of that, I have to define a pattern for error matching. Now, Hy gives quite a large error spew, for example: ...
7
votes
1answer
1k views

In regular expressions, what's the difference between [:blank:], [:space:], and \s-?

I've been playing around with rx lately, and I just noticed there are (at least) 3 different ways of indicating whitespaces in regular expressions. [:blank:] [:space:] \s- What are the differences ...
5
votes
1answer
1k views

Why are double quotes not escaped in the replace-regexp command?

I have text like this: myvariable = ABCD, EFGH, IJKL, ~subsection1 MNOP, QRST, UVWX, YZ Which I want to transform to: myvariable = "ABCD," "EFGH,\n" \ "IJKL,\n" \ "~subsection1\n" \ "MNOP,\n" \ "...
2
votes
3answers
168 views

Convert regexp list so that each member has word boundaries

I have several lists with regexp matching categories of file extensions: (let* ( (txt-regexp '("txt" "md" "log" "org" "conf" "rtf")) (el-regexp '("el" "lisp")) (tex-regexp '("tex")) (...
3
votes
1answer
161 views

Composing a regexp to highlight folder extensions differently

I am having difficulties composing a regexp that will highlight certain folder extensions -- including the preceding dot -- with colors that are different than the rest of the folder -- e.g., .app ...
31
votes
2answers
5k views

Understanding of emacs align-regexp

I've read the Emacs documentation for align-regexp but still have difficulty in understanding how it works. What I'm talking about is its prefixed form C-uM-xalign-regexp, not the simple form M-xalign-...
14
votes
3answers
3k views

Is there an equivalent to sed-style substitution commands from vim?

One of the things I miss from vim is being able to type in a substitution command that will work over multiple lines, for example: :/begin/,/end/s/foo/bar/g The above command substitutes "foo" for "...
15
votes
1answer
711 views

How do I create a dynamic regexp with rx?

I want to use rx to create regular expressions with runtime values. Currently, I'm doing this: (setq strings '("foo" "bar" "baz")) (eval `(rx symbol-start (or ,@strings) symbol-end)) However, I'd ...
6
votes
1answer
660 views

Regexp replacement: difference between \([[:digit:]]+\) and ([[:digit:]]+)

With M-x replace-regexp, I found that both \([[:digit:]]+\) and ([[:digit:]]+) will match (123), and not one without parentheses, But the first will replace 123 and the second (123). I wonder why the ...
12
votes
7answers
1k views

How can I practice searching and replacing with regular expressions interactively in Emacs?

Searching and replacing using regular expressions is clearly a powerful tool to the Emacs user who can routinely perform these actions. However, as an Emacs (and/or programming) beginner, regular ...
15
votes
2answers
593 views

How can I test and use a Perl regular expression interactively?

Say I have a buffer with text. I would like to test a regular expression (ideally Perl type) against my buffer and have Emacs highlight the matches on it. For example, the following regexp (taken ...
9
votes
1answer
719 views

Align vertical columns of numbers on the decimal point

I wrote this post in Emacs. In order to make the tables look nice, I decided to adjust the spacing so that decimals line up neatly. The original format from the database looked like this: emacs ...