As of May 31, 2023, we have updated our Code of Conduct.

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
2 votes
1 answer
581 views

highlight-regexp groups for syntax highlighting

I want to highlight certain words to a color using highlight-regexp. The words I want to highlight are "fn", "let", "mut", etc. for rust development. The code I have so far is this: (add-hook '...
Vent's user avatar
  • 77
6 votes
3 answers
976 views

Regexp replace to match a string, but not match a superstring

Let's say I want to replace the string "Vector" by "VectorBase", but there are existing instances of "VectorBase". So I would like to omit "VectorBase". What is an elegant way to achieve this? An ...
hrkrshnn's user avatar
  • 429
5 votes
1 answer
428 views

How close and exit from buffer *Re-builder*?

emacs 26.1 M-x regexp-builder Input some regexp. Nice. But now I want to close and exit from buffer Re-Builder. How I can do this?
a_subscriber's user avatar
  • 3,830
0 votes
2 answers
92 views

regexp-builder - no match with correct regexp. Why? [duplicate]

In buffer I has text "ADA-SGD" "ADT-SGD" "ADX-SGD" "AID-SGD" "AMP-SGD" "ANT-SGD" "ARDR-SGD" "ARK-SGD" M-x regexp-builder Input regexp: "\([A-Z]+\)-\([A-Z]+\)" But I get message: No match. Why?
a_subscriber's user avatar
  • 3,830
0 votes
0 answers
78 views

Has matches , but re-builder print "No matches"

Emacs 26.1 In scratch buffer I has text KRW-EOS hel-how hello-how-ar h-privet kak-del 123-567 aaa-bbb AAA-BBB AAAA-BBBB AAA-BB AA-BBB A-A AAAAAAAAAA BBBBBB AAA BBB ZZZ-YYY M-x regexp-builder Input ...
a_subscriber's user avatar
  • 3,830
0 votes
1 answer
308 views

How to match a quoted symbol in a string in elisp?

I'm struggling to find a correct regexp to match quoted symbol inside a string: " 'foo " or " 'foo.bar " Any help will be appreciated
szymanowski's user avatar
0 votes
1 answer
70 views

How remove text before "," (include) in every lines?

suppose I has the next text: -1,0x0 Fireplace HD 1160,0x0 Music HD 719,1 HD -1,1 MUSIC CHANNEL 154,1+1 -1,100% News -1,100AutoMoto TV I need to remove text before "," (include ,) So the result must ...
a_subscriber's user avatar
  • 3,830
1 vote
1 answer
126 views

How do I align code on dot/period (.)?

I recently switched to emacs. Just wanted to see how it works and if it'll help with productivity. Right now I'm working in Haskell, which means a lot of function composition with . operator. I have ...
atis's user avatar
  • 113
1 vote
1 answer
46 views

Adding a line to the beginning of every function in a (ruby) source code file

Supposed I have a source code file (classes) and some methods in that file. see https://github.com/railstutorial/sample_app_rails_4/blob/master/app/controllers/users_controller.rb I want to add a "...
american-ninja-warrior's user avatar
0 votes
1 answer
61 views

Emacs Regexp syntax clarification

I am a bit confused on some aspects of Emacs regexps. Specifically with the paragraph on this page ‘\W’ matches a character which is not within a word ‘\B’ matches characters which are not a ...
smilingbuddha's user avatar
4 votes
1 answer
11k views

Whitespace and newlines in regexps?

For a number of reasons (one of them my lameness) I get extremely confused when it comes to whitespace and newlines in regexps. Say for example that I want to search for an equal sign (=) followed by ...
Joakim G.'s user avatar
2 votes
1 answer
573 views

Tramp Unable to Recognize Remote Prompt

I have been trying to get tramp working and I am having issues with recognizing the remote prompt. Unfortunately, I cannot change the remote prompt because it is dictated by my school. I have tried ...
Matt Daly's user avatar
1 vote
3 answers
267 views

Function that does `M-x replace-regexp` or `M-x occur` with a fixed pattern and replacement strings

I often use M-x replace-regexp and M-x occur with some fixed regexp pattern and replacement string on many files. Doing it manually by opening each file in an Emacs buffer and hitting the keys is ...
Tim's user avatar
  • 4,947
0 votes
2 answers
106 views

font-lock limits ignored in anchored matchers

I think I might have found a bug in GNU Emacs but I want to check my understanding first. In Search Based Fontification it is possible to specify a function as a matcher. It must obey the contract ...
fommil's user avatar
  • 1,730
0 votes
1 answer
189 views

Advanced search and replace

I have a file, with a lot of instances of ... fclose(f1); fclose(f2); fclose(f3); ... I'd like to replace these with ... if(fclose(f1)!=0) return 1; if(fclose(f2)!=0) return 1; if(fclose(f3)!=0) ...
Mikkel bruun's user avatar
0 votes
1 answer
291 views

forward-sexp confusion

The following code is an excerpt from the elisp tutorial, section 14.6. (while (re-search-forward "^(defun" nil t) (setq lengths-list (cons (count-words-in-defun) lengths-list))) Placing the ...
zugzwang's user avatar
  • 319
0 votes
1 answer
63 views

What's the regex to replace <foo> with FOO?

I have a buffer with a lot of instances of words surrounded by '<' and '>', and I want to replace them all with the just capitalized word. E.g. <foo> -> FOO. I read the regex chapter of the ...
NTC's user avatar
  • 25
1 vote
1 answer
1k views

emacs regex to match balanced parenthesis

How can emacs lisp match balanced parenthesis? for example: "foo(bar(),hee()) out()" should be match "foo(bar(),hee())". I try below statement but it doesn't work: #!/usr/bin/env emacs --script (...
beetlej's user avatar
  • 1,056
0 votes
1 answer
2k views

elisp regex newline escape confusion

Shown below is a simple function, which was run in the middle of a text file that contained multiple instances of repeated blank lines. The output of the function is also shown below. The only ...
zugzwang's user avatar
  • 319
2 votes
3 answers
471 views

Highlight regexp case insensitive

Is there a way to use highlight-regexp in a case-insensitive way? I'm parenthesizing an expression in my regexp and refer to it using \1 later and I want to match this regardless of case. I'm trying ...
Alfe's user avatar
  • 121
2 votes
2 answers
313 views

re-search-forward unexpected behavior: case insensitive

According to the documentation of this function, it should be case sensitive. But when I test it out, the result is case insensitive. My emacs version is 25.3.2.
Cảnh Toàn Nguyễn's user avatar
0 votes
1 answer
175 views

regex to match abc = abc statement

Use emacs-lisp, how to match the same variable assign statement? for example: below lines should be matched: abc = abc xyz = xyz weeoof = weeoof But below lines should not be matched: abc = abd ...
beetlej's user avatar
  • 1,056
0 votes
1 answer
873 views

How can I specify a regex pattern which matches only lines without a specific string? [duplicate]

How can I specify a regex pattern which matches any line without "lecture" anywhere in it? ".pdf" at the end?
Tim's user avatar
  • 4,947
3 votes
1 answer
2k views

emacs regex with multiple match for text, in multi line buffer

Following is sample text. Header This is test, and mid line adsf bag a lot of many things shoes> shoes/ This line should not be affected. Neither this line. This is test, and mid line bag a lot of ...
msinfo's user avatar
  • 177
4 votes
1 answer
1k views

Generate a date tree from timestamps in org-mode

Since I was interested in doing my early capture-templates, I was tempted to go with this template below: ("j" "Reference type: Journal" entry (file+headline "~/Dropbox/orgfiles/gtd/ref.org" "...
doctorate's user avatar
  • 1,759
0 votes
1 answer
167 views

use a function to get file in compilation-error-regexp-alist

I have used compilation-error-regexp-alist a lot so I am familiar with the syntax. However for this particular error I need to use a function to get the file because I need wildcards. Here is the ...
Prgrm.celeritas's user avatar
0 votes
2 answers
119 views

Create strings of a wordlist using replace-regexp

I have a word list with a lot of words that I want to convert into a json file. To do this I need to make them strings and add a comma in the end of all words. I figured it would be nice to use ...
Salviati's user avatar
  • 133
0 votes
1 answer
130 views

Using a predefined Regex on a yank

can you please help me to write a small emacs function? This is the first lisp function I am writing and I think I am getting confused with point and mark placement. I want to write a function that ...
Honigmelone's user avatar
1 vote
0 answers
154 views

Change regex-builder-mode hook to use <C-s>

I recently discovered emacs's "regex-builder" mode and am absolutely loving it. However, there doesn't seem to be an easy way to type in an expression in the "RE-Bulider" buffer and use that ...
Ricardo Iglesias's user avatar
1 vote
2 answers
130 views

Removing and adding newlines around expressions

I am in the process of formatting a couple of hundred markdown files and am trying to automate that process. I am having two problems, but I guess I can ask two questions ;) Goal I am trying to ...
Øistein Søvik's user avatar
4 votes
1 answer
688 views

Find and remove consecutive duplicated words while ignoring case

I am trying to find and remove duplicated words in a LaTeX file. Right now I am using query-replace-regexp with the regular expression \b\(\w+\)[ #]+\1 where # stands for line-break (C-q C-j). This ...
Tássio's user avatar
  • 209
1 vote
2 answers
967 views

How to extract some text from within HTML code?

I have this string in an elisp variable <br /><b>Country</b>: United States<br /><a href I want to extract out the United States in to a separate variable. How can I do ...
american-ninja-warrior's user avatar
0 votes
1 answer
109 views

Function that returns the name of a Greek vowel

I'm trying to write a function that will return the name of a Greek vowel: alpha or epsilon etc. I was hoping this could be done by extracting it from the second group in the following regexp (in a ...
Toothrot's user avatar
  • 3,114
0 votes
2 answers
86 views

Modify regexp in function to match only capitalized words

I have the following function (defun fphp/find-unimported-classes () (interactive) (mapcar (lambda (classentry) (ov-clear (classentry-overlay classentry)) ) ...
Filip Górny's user avatar
0 votes
0 answers
177 views

Interactively matching beginning of region

Short version: If i bound the scope of a search/replace to a region in the buffer (via the second/third optional argument of search-forward and its derivates), how can i match the beginning of that ...
Lupino's user avatar
  • 121
1 vote
0 answers
52 views

JavaScript regular expressions in re-builder

Is there any package that allows me to input regular expressions following the JavaScript syntax (or even Perl Compatible Reg. Exp.)? I want to have an interactive regular expression builder (like re-...
rph's user avatar
  • 315
1 vote
1 answer
4k views

Matching a literal asterisk (*) in a regexp

I am trying to write a regexp to mean: At least one of a few punctuation marks followed by any number of blank spaces followed by a literal star is to be replaced by all those punctuation marks ...
deshmukh's user avatar
  • 1,842
0 votes
1 answer
61 views

How to bypass lines matching some regexp, when searching forward

I've written the following function to go from an error line to the next one: (defconst mylog-mode-error-regexp "\\(\\[ERROR\\]\\|rule did fail\\)" "Regexp to recognize errors in my log file.") (...
user3341592's user avatar
0 votes
1 answer
524 views

How to use flush-lines in a script?

I want to use flush-lines in a selected region, but I have troubles. Here it is my code: (defun delete-empty-lines () "Delete empty lines in bibliography." (interactive) ;; set point-min (...
Onner Irotsab's user avatar
1 vote
1 answer
437 views

Produce \' in a string replacement

As you know, functions such as replace-regexp-in-string and replace-match have an argument LITERAL, meaning ... If optional ... arg LITERAL is non-nil, insert NEWTEXT literally. Otherwise treat ‘\’ ...
user1404316's user avatar
6 votes
1 answer
947 views

How can I use more than 9 regex capture groups in Emacs Lisp?

I have to do a regexp replacement with more than 9 capture groups. How can I do it? Here there is my code (with \\10 and \\12 that they do not work as I expected to): (perform-replace "\\\\href{...
Onner Irotsab's user avatar
0 votes
1 answer
210 views

Org todo list: Filter regexp

When I press C-c a M and search for a certain tag, I get a todo list of all open tasks for this specific tag. Is it now possible to do a regular expression search similar to the sparse-tree regular ...
Michael's user avatar
1 vote
1 answer
516 views

Why do regexp that matches text in buffer does not (necessarily) match same text in string?

I have a regexp, org-parse-regexp which matches org items which I define to be a heading and a source block (actually, I include more stuff but I want to keep it simple). I've tested it using re-...
Aquaactress's user avatar
  • 1,383
2 votes
1 answer
413 views

Jump to and highlight characters matching regex

How can I jump to the next character matching a regex (see example below)? And if possible, enable highlighting of all the matching characters in the buffer when I jump for the first time. [^\x20-\...
forthrin's user avatar
  • 451
5 votes
2 answers
2k views

How to toggle regex for helm/ivy user input?

IIUC, all helm commands (including helm-swoop, swiper-helm) have regex enabled by default for user input. Can we toggle/disable it by some command/shortcut? Let me know if I missed something. Using ...
AhLeung's user avatar
  • 1,063
0 votes
1 answer
133 views

case-fold-search apparently not taking effect in re-search-forward

I wrote the following command to do a regexp replacement in a region. (defun camel-case-to-underscore (a b &optional use-dash) (interactive "r") (save-excursion (let ((sep (if use-dash "-...
erjoalgo's user avatar
  • 833
16 votes
1 answer
8k views

how do I quickly remove lines from emacs buffer

In an emacs buffer when editing a file called "log/development.log" how do I quickly remove all lines containing the word "Render"
american-ninja-warrior's user avatar
1 vote
3 answers
1k views

Help with `replace-regexp-in-string`, to replace only first instance in every line

I have the following string: // const asdf = 'asdf' // // comment // const asdf2 = asdf And I want to run a regexp replace to make it look as follows: const asdf = 'asdf' // comment const asdf2 = ...
modulitos's user avatar
  • 2,402
2 votes
1 answer
30 views

turn coherent string into individual characters

I have a text with a number of coherent strings, e.g.: ax923fasd asidna1 xxa008 Is there a quick way to convert these so that there be a space between each of them, i.e.: a x 9 2 3 f a s d a s i d ...
miri sueß's user avatar
3 votes
2 answers
301 views

re-search-forward through visible text only

I have one big org file. Sometimes I only want to search for a regular expression through the visible text, not the text that is folded; otherwise my search will take much longer. How can I search ...
Aquaactress's user avatar
  • 1,383

1
3 4
5
6 7
10