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.
323
questions
1
vote
2answers
125 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 ...
0
votes
2answers
68 views
What is the equivalent of “\n\t ” in string mode in re-builder?
This webpage recommends being in the string mode when using re-builder, the interactive regexp builder, where one does not have to use "\" to escape special characters.
But how does one convert ...
0
votes
1answer
100 views
How to define abbrev boundaries and where to put them?
I'm new to Emacs and Common Lisp. I'm wanting to use abbrev-mode to replace standard Common Lisp function names with aliases that I have created. So for example, I would like to replace the following ...
1
vote
2answers
207 views
How can I use Regexp to rename files in Dired mode?
I've have some videos (.mp4) with subtitles files (.SRT) that I need to rename. These files have long names so I can only rename them manually. I've tried Python to rename them, but it fails.
I've ...
2
votes
2answers
121 views
Syntax table to match arbitrary forms with regexp
I'm trying to write a small major-mode for editing pico-8 files in emacs and I had a quick question regarding best practices and matching a somewhat arbitrary form in the pico-8 cartridge files for ...
2
votes
2answers
1k views
Display another symbol than asterisks in org headings
I would like to change the way headings render while still using the * to type headings (so this question is distinct from Change the asterisk (heading identifier) to some other symbol in Org mode)
I ...
1
vote
1answer
140 views
how remove all but the first n chars
How can I remove all but the first n characters of a every line in the file buffer?
What I tried
Mark region
C-x r k #but this is cumbersome when the length of lines are very varied, and there are ...
1
vote
1answer
252 views
How can I match a tab not at the beginning of a line?
In Emacs, when using M-x replace-regexp, how can one specify matching a tab which is not at the beginning of a line? I know how to match a tab which is at the beginning of a line by using ^, but don't ...
12
votes
2answers
872 views
Incrementally replace a given string
Let us say I have a text like so below:
AC(nn)
AC(nn)
AC(nn)
AC(nn)
AC(nn)
AC(nn)
AC(nn)
AC(nn)
AC(nn)
AC(nn)
AC(nn)
Now I want to replace the nn with numbers like so
AC(0)
AC(1)
AC(2)
AC(3)
AC(4)...
1
vote
1answer
209 views
Emacs Compile Mode: Extra characters before filename result in bad match
In the compile buffer of Emacs 25.3.1, I am getting the following compilation error using Clang (the error message is given as it appears in the compile buffer line by line) :
src/platform/linux/...
2
votes
1answer
65 views
Why does ^#.*#$ not capture recursively inserted files?
While considering some refactoring, I made changes throughout a project.
Halfway through the changes, and before saving, I found it doesn't quite work. I exited emacs and restarted. (That's probably ...
1
vote
0answers
42 views
How to set ps-end-cut-regexp
I often use ps-print-buffer to print out files, but I don't want the first few lines of the buffer to be printed. Here's the beginning of the buffer:
# -*- mode: org; -*-
# Last updated: <2017/12/...
0
votes
0answers
112 views
Prettify string, find by regex
I want to replace the regular expression `.*` by bolding .* and not displaying the backticks. So I want `elem` to become elem
I looked into these questions about how to prettify strings rather than ...
3
votes
1answer
355 views
Find/replace in differrent file extensions
Windows 10, Emacs 25.1, Dired+, xah-find
I use package xah-find for find/replace text in many files.
OK. It's work.
If I want to find in all java files I use the next regexp:
File path regex: \.java\...
2
votes
1answer
166 views
highlight different regexp groups with different colors
Is there a way, that each group in regexp can be highlighted with a different color.
For example, if I am searching for
\(text_part_1\)\|\(text_part_2\)
Can the text_part_1 be highlighted in one ...
1
vote
1answer
56 views
ibuffer hide beginning of lines
When I press C-x C-b I get ibuffer (installed as part of prelude I think)
I like to press backtick and get the Filename/Process displayed.
Another SE answer helped me with that. (ibuffer hide columns)...
0
votes
1answer
75 views
Strange regex behavior
I want to skip blank region under cursor, so I created this regex:
\s+\w translated to emacs regex [:space:]+[:graph:] but it doesnt match what I want. It matches some random space somewhere!
Whats ...
1
vote
1answer
69 views
Executing multiple replacement regexps against a string
I wanted to write a function that "auto-corrects" a string, based on a list of regular expressions and their respective replacements.
I've come up with this, and it works:
(setq auto-corrections-...
0
votes
1answer
94 views
Using result of an Elisp function as input to replace-regexp in interactive mode
I am using replace-regexp interactively. Can I programmatically refer to a match or otherwise interpolate values in the replacement string? In Ruby, for example, it is easily done, by using the #{ <...
1
vote
1answer
146 views
Exact match for isearch
I am relatively new to emacs. I am looking for a way to do the "exact match" type of interactive search. For example, search string "abcDEF" in following text should only match with line1's text:
...
1
vote
1answer
126 views
elisp replace float number with half value
I need to replace below string:
(24.0000,28.0000) .. controls (27.3100,28.0000) and (29.9800,25.3100) .. (29.9800,22.0000)
to:
(12,14) .. controls (13.655,14) and (14.99,12.655) .. (14.99,11)
The ...
4
votes
5answers
285 views
find end of string constant
I'm using a simple (re-search-forward "\"") to find the end of string constants in my buffer. This is to copy the string in a def like (defconst name1 "some string"). The problem is that string ...
3
votes
2answers
162 views
Query-replace-regexp alternative capable of handling nested braces?
TL;DR
Is there some method in Emacs to replace text with nested braces/parentheses?
Example
I had a situation where I had to replace
\dchi{ARG1}{ARG2}
by
\chi^{ARG2}(ARG1)
For simple cases, ...
8
votes
2answers
452 views
Search through the values of all variables in Emacs
I'd like to find which variables contain a certain value.
Is there a way to do a regex search through the values of every variable in Emacs?
2
votes
2answers
218 views
Enable mode if file content contains a matching string
I would like to enable rjsx-mode whenever a file I open contains a specific string, or a regex like import.*react for example. Is there a way to do something like this in emacs?
0
votes
1answer
59 views
In emacs while using M-x replace-regex, the expression matches except when i put a ^ and $ at the beginning and end. This is confusing to me!
The search query is : ([\s]*print)[^(](.*)
It matches, BUT when I put ^ and $ like this :
^([\s]*print)[^(](.*)$, it doesn't.
Again both work in search-forward-regex but the 2nd one does not work in ...
2
votes
2answers
189 views
Make flyspell avoid checking #includes in C++
Flyspell does a great job ignoring all the source code and only check comments, with one flaw: it still does check the text after #includes. So the iostream here does get highlighted:
#include <...
2
votes
1answer
113 views
Multiple URL formats for bug-reference-mode
I’m working on a project which is version controlled using GitLab. GitLab, unlike GitHub, uses two separate URL endpoints for issues and merge requests (AKA Pull Requests):
https://gitlab.example....
0
votes
1answer
205 views
elisp multiple line match string regexp
To match with multiple line regexp, I can use \\(.*\\|\n\\), but I think it should be [.*\\|\n\\] which actually doesn't work as expected.
#+NAME: exam1
#+BEGIN_SRC elisp :results output
(let ((str ""...
1
vote
2answers
131 views
Does rx macro work with underscore in character classes
I'm a little confused by the fact that underscore does not shown in the character class.
ELISP> (require 'rx)
rx
ELISP> (rx (any "A-Z" "_"))
"[A-Z_]"
ELISP> (rx (any "A-z" "_"))
"[A-z]"
...
7
votes
1answer
522 views
Is there any principal difference between “A-Z” and upper?
I just want to make sure I understood this correctly.
(rx (one-or-more (any upper lower)))
is equal to
(rx (one-or-more (any "A-Z" "a-z")))
Correct?
1
vote
1answer
121 views
How to make rx.el regexps non greedy?
Here is my expression:
(setq myregexp (rx bol
"####"
(one-or-more anything)
"."))
This should match any string starting with "####", spanning ...
1
vote
2answers
30 views
M-x replace-regexp uppercases words in replacement text based on the first letter present in the line
So, I have the following buffer:
1234
Red
blue
green
yellow
# colors
orange
Now, if I run M-x replace-regexp I get odd results. If I have the search string: ^\(.+\)$ and the replacement string: ...
2
votes
2answers
200 views
Rx: Skip commented out lines
I'm trying to make an expression that would match the text foo if it's not on a commented out line.
;; foo ; <= not this
bar ; <= not this
foo ; <= this
Now rx.el can exclude ...
1
vote
1answer
638 views
How can I make a custom font-lock keyword regex?
I'm new to emacs (spacemacs) and have been trying to get org-mode to highlight quoted strings "like this" and quoted strings 'like this'. I followed the example here, and came up with this:
;; ...
2
votes
2answers
2k views
Regex: Only match base.extension filenames
I would like to match all filenames with only one dot in them:
(re-search-forward "[a-z0-9A-Z]?+\\.dsp$")
bdsp
plop.dsp.zzz
plop.1.dsp
plop.dsp
Here, the regexp matches plop.1.dsp, but it should ...
0
votes
0answers
68 views
Why does looking-at ignore the *?
I seem to have some misunderstanding with looking-at. This is my test
function :
(defun lkat ()
"interactive looking at"
(interactive)
(if (looking-at "^\\s-*")
(message "yes")
(...
1
vote
1answer
96 views
Passing a string with substitutions to the “perform-replace” command
I want to generalize this query-replace regexp:
(perform-replace "Geophysical[\s\t\n]*Research[\s\t\n]*Letters"
"Geophys.\\\\ Res.\\\\ Lett."
t t nil 1 nil (point-...
1
vote
3answers
91 views
Finding Numbers in Brackets and Summing Them
QUESTION:
How can I make an emacs command M-x sum-the-brackets that scans highlighted text for all occurrences of [ <number> u ] and [ <number>u ], and sums them?
EXAMPLE:
Selecting ...
1
vote
0answers
148 views
Help Aligning a Pattern with `align-regexp`
How can I use M-x align-regexp to align by
//
All ; after //
All , within [] after //
For example:
With text:
Line 1 ; B // Comment 1 [11, 22] ; Comment 2 [3333, 4]
Line 2 C // Comment 3 [3, 55] ...
3
votes
1answer
148 views
Arithmetics in a regex in a lisp function
I want to replace all hard column widths in several LaTeX-documents, e.g. replace {90mm} by {0.75\textwidth}. I already found out that you can use \, and #1 to so arithmetics on captured groups, like ...
1
vote
3answers
204 views
Regexp only matching one character when using abbreviations
I'm trying to expand abbreviations with some additional characters, so I'm using the regexp option on an abbrev-table to try and add these characters. The expression I used was found on this tutorial ...
3
votes
3answers
3k views
Regex for any file not ends with
How to write a function for finding all files in a directory which not ending with pyc and pyo?
I've tried something like (find-lisp-find-files root "^(?!.*pyc$).*$") but it does not return any ...
1
vote
1answer
161 views
emacs align-regexp
I'm looking for a way in emacs to format a code like this:
// param1, param2, param3, param4
foo(9, 8, "bar", 3)
to this:
// param1, param2, param3, param4
foo(9, 8, "bar", 3)
I guess ...
1
vote
1answer
269 views
replace checkbox with glyph via font-lock
I am trying to figure out how how to replace structural elements in org-mode with glyphs/abbreviations to make them more readable. Unfortunately, I'm getting stuck on replacing a simple checkbox [X] ...
1
vote
1answer
403 views
regular expression whole word replace, whitespace only
Some modes treat underscores as word boundaries.
I am using evil via spacemacs. I am trying to use a vim/sed style :%s/foo/bar/g search and replace, but operate only on whitespace delimited words, ...
0
votes
1answer
134 views
Capture text after an OR group in Emacs Lisp
Say I want to match the a character "h a" or "f a". So capture all the a that are preceded by an h or an f.
I tried the following :
1) \(h\|f\) \\(a\\)
but it doesn't work. the problem appears to ...
1
vote
1answer
227 views
sort lines by regexp
I have a todo list in a text file that I maintain with TaskAgent and emacs. TaskAgent adds trailing text to a line to indicate priority (#!!! represents highest, #! represents low, and no mark is ...
1
vote
2answers
646 views
Is there a way to reuse patterns in a RegExp?
Somethimes I need a RegExp which uses a complex pattern multiple times. If I copy the same pattern there it works, but this is error prone as I then need to change each occurence of the pattern ...
12
votes
1answer
2k views
support for regex look behind and ahead?
I need to perform regex query replace, such that
foo in foo bar is matched, but foo in foo baz is not. Normally I would use regex look ahead, e.g. foo(?=bar).
However, it seems like Emacs cannot do ...