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
4
votes
3answers
1k views

Search for and correct capitalized words that are not at the beginning of a sentence

I have a latex document in which there are many incorrectly Capitalized words. Could anybody suggest a clever regex to track them? Even better if there is a function which cycles through such words ...
2
votes
3answers
804 views

Why doesn't “$^J” match a new line character after another line?

I want to match two or more consecutive lines with numbers only, e.g.: abc 1 2 abc In Emacs, my regex is \(^ *[[:digit:]]+ *$^J\)\{2,\}, but it doesn't match any. I find \(^ *[[:digit:]]+ *^J\)\{2,\}...
4
votes
1answer
138 views

Can I apply a face to an optional group?

When I try to execute the following, font-lock fails abruptly after it matches the first group. A little bit of debugging with the neat tool font-lock-studio and I realized that font-lock is upset ...
1
vote
2answers
446 views

Regexp to font-lock numbers but not strings?

I would like to have my dollar sign yellow coloured. So after searching around, I learnt that the Emacs regex's are different from default regexs. After a while, I got this worked: (defface font-...
6
votes
1answer
180 views

How can I fontify a symbol by regexp match group (as in a naming convention)?

I want to add better support for expl3 to AUCTeX. I have regular expressions that properly group according to the naming conventions for: functions (\\(?:__)?[a-z]+_)([a-z_]+)(:[nNpTFwcVvxof]*), ...
0
votes
1answer
479 views

Revert-without-query, regex pattern error

(setq revert-without-query '("\*")) I have set this to avoid emacs' yes/no prompt when i revert a buffer and discard its changes. But, still the prompt keeps coming. Other regex patterns, i tried ...
1
vote
2answers
193 views

Is there any way to configure isearch-forward-regexp invert backslashes usage?

It may be handy to make it shortly, for example use (GET|POST): *\{ instead of \(GET\|POST\): *{
0
votes
1answer
300 views

Imenu Generic Regexp for CSS / LESS

I am trying to create a Regex for Imenu indexing for my CSS modes but I'm having trouble getting it exactly right as I'm no regular expression guru. Here's a snippet of LESS CSS I'm using (regexp-...
0
votes
1answer
84 views

Error escaping elisp regex

(string-match-p "^\(./\|../\)" "../lib/tern") returns false. What is the correct regex pattern to check if the string begins with ./ or ../?
33
votes
2answers
304k views

What is the regex to match a newline character?

In Emacs regex, \n doesn't match a new line character \n. Am I correct that $ matches the position between a new line character and the character right before the new line character. e.g. for a ...
2
votes
2answers
119 views

Replacing every empty line with a string, i.e. inserting strings to empty lines

Given that I have a file such as Lorem Ipsum Dolor I want to insert strings (here, the % character) to those empty lines to obtain Lorem % % Ipsum % Dolor How can I do this? I tried to follow a ...
3
votes
1answer
633 views

Turn case sensitivity off in regular expression replacement

Converting a subversion repo to git, and setting up an authors file. I'm running query-replace-regexp on this text: E001234 using this regexp: ^\(.*\)$ -> \1 = Some Lowercase Text Here <email@...
4
votes
0answers
145 views

LaTeX-aware sentence endings?

Q: how do I define sentence endings to be LaTeX macro-aware? I'd like backward-sentence and forward-sentence to be smarter about LaTeX macros -- particularly \footnote -- when working in LaTeX ...
2
votes
1answer
329 views

Custom Major Mode - Regex to find word before equal sign and set font-lock-variable-name-face

I'm trying to create my own major mode. So far I've been able to set all the syntax highlighting that I need, but I'm struggling with this one. Basically I need to color any word before an equal sign....
4
votes
2answers
268 views

Global goto regex?

I would like to run a command and go to the first matching regex pattern that matches in any open buffer. Kind of like search-forward-regex. The requirements are: not be limited to forward/...
2
votes
1answer
476 views

Repeat regexp from isearc-forward-regexp on a subsequent replace-regexp?

I use C-M-s to search for a regexp. Then I use M-x replace-regexp to replace the same regexp with some string. Can I save typing the regexp for the second time? Generally, when typing an argument ...
6
votes
3answers
1k views

How can I build a regexp insensitive to case?

I have a bunch of keywords: set write quit which each can be shortened down to one letter: s se set w wr wri writ write q qu qui quit Further, these keywords are case-insensitive, so the following ...
5
votes
4answers
357 views

Is there a way to search/replace with regexps interactively using rx syntax?

I really like rx but I can't use it interactively. Has someone already tried to extend isearch to support rx syntax?
14
votes
2answers
551 views

Does elisp have regexp literals?

The sheer number of backslashes my regexps require is pretty crazy. Does elisp have regexp literals, so I can write something like rx"some\(regexp\)" instead of "some\\(regexp\\)"
10
votes
1answer
2k views

How to search an Arabic word in text without its diacritics/accents?

In Arabic as with some other languages there is what is called diacritics to enhance pronunciation. There is no convention on how many diacritics should be written for a single word. Some use the ...
1
vote
1answer
128 views

Regexp not working

I messed up my latex code; I need to replace \cite[42]{source}. with .\cite[42]{source} and \cite{source}. with .\cite{source}. I tried \\cite\(.*\)\}\. -> .\\cite\1\} but I got an error: Invalid ...
1
vote
1answer
2k views

How to match the function name in a function call?

I am trying to extract the function name from a function call. For example: (let ((str "(test-func")) (if (string-match "^(\\s_+" str) (message "ok") (message "no match"))) But this ...
2
votes
1answer
697 views

Capitalizing letters that follow colons using 'replace-regexp-in-string'

I want to capitalize letters that follow colons. Here is the code: (replace-regexp-in-string ": \w" 'upcase ": really") The result I expected is : Really. Instead I get : really. Similarly, this ...
2
votes
2answers
89 views

Find the next occurence of lines with matching prefix

I've got a file that looks roughly like this: prefix.thing,11,12 prefix.something,10,20 prefix.something,30,40 prefix.somethingElse,10,20 prefix.anotherThing,1,2 prefix.anotherThing,3,4 assuming my ...
1
vote
5answers
18k views

regular expressions: matching all words containing a specific list of letters

The regular expression \ws\|l\|d matches all words containing at least one of the letters s, l and d. What is the correct syntax to match all words containing all these letters? (like solid, dollars, ...
4
votes
3answers
598 views

Dired: can't create a regexp to hide all the necessary files

I read the following article: http://qerub.se/hiding-hidden-files-in-emacs-dired about hiding certain files in dired mode. I decided to hide more files and added a shortcut Alt-q, that will toggle the ...
3
votes
4answers
508 views

Sorting words with hyphens (e.g. in a Lisp mode)

SortWords in the EmacsWiki suggests this function as a way to sort words: (defun sort-words (reverse beg end) "Sort words in region alphabetically, in REVERSE if negative. When prefixed with ...
2
votes
1answer
340 views

Eshell prompt regex

I'm trying to get my two-lined eshell prompt working with eshell. It gets displayed alright, and it's also non-readable. But when I call something external, the cursor doesn't jump "down" to the new ...
22
votes
6answers
8k views

Get all regexp matches in buffer as a list

On the Code Golf Stack Exchange site today, I found this answer in Clojure to the question "Get all links on a webpage". (->> (slurp "http://www.stroustrup.com") (re-seq #"(?:http://)?www(...
3
votes
1answer
206 views

How do I highlight certain words between double quotes with font-lock?

I'm trying to add additional colors to json-mode for when I edit json-schema. I can get it working fine without searching for double quotes, but I need to for some highlighting. I've tried quite a ...
4
votes
2answers
700 views

How to make this RegExp less greedy?

I use visual-regexp to make query regexp search. Consider this text: \enquote{\Large{text1}} \enquote{\Large{text2}} \enquote{\Large{text3}} \enquote{\Large{text4}} I want regexp to match every and ...
8
votes
3answers
747 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
976 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
72 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
4k 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 ...
29
votes
1answer
5k 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
293 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
1k 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_ --&...
7
votes
1answer
402 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
297 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
201 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
2k 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
197 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")) (...
4
votes
1answer
184 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 ...
34
votes
2answers
7k 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-...
13
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
984 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 ...

1
5 6 7
8
9