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
1
vote
1answer
175 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
344 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
220 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, ...
9
votes
2answers
971 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
357 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
324 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 <...
4
votes
1answer
227 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
393 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
196 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
557 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
185 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
35 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
288 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
1k 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
3k 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
72 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
127 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
110 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
212 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
171 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
263 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
4k 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
342 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
512 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] ...
3
votes
1answer
562 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
156 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
348 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
771 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 ...
16
votes
2answers
3k 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 ...
0
votes
1answer
237 views

How to highlight other square brackets in custom major mode?

I'm working in a custom major mode, and part of the syntax are the square brackets, like [Hello world] and so they can contain nested square brackets as in [Hello [World]], however I see this problem ...
1
vote
0answers
57 views

using re-search-forward in yas-snippet

I'm trying to develop a new yas-snippet for expanding a common c++ log function I use a lot. The first parameter to the LOG function can be guessed by searching for existing LOG calls in the buffer. ...
1
vote
1answer
194 views

Match blank lines at end of file

How can I match – in, for example, a group of files from a Dired buffer – all files that have one or more blank lines at the end of the file? The specific need that prompts this question is to remove ...
3
votes
1answer
382 views

Yasnippet including backslash

I would like to use Yasnippet to insert the template for a latex inline equation: \( \) The snippet is defined: # -*- Mode: snippet -*- # name: inline equation # key: ineq # -- \( \) But ...
1
vote
0answers
68 views

Can you use a regexp in an abbrev definition?

I want to replace words like 1st with 1^{st} and 102nd with 102^{nd} while typing in emacs. Basically I want to replace things that match this: "\([0-9]+\)\(st\|nd\|rd\|th\)" with this \1^{\2} as I ...
2
votes
1answer
412 views

calling search-forward function with ^ and $ in the regexp pattern

I want to create a table in org-mode and use it in Ditaa The following function is created to replace “^+-” to “|-” and similarly, “^-+\s-*” to “-|”. Surprisingly, this does not work. (defun ditaa-...
2
votes
1answer
683 views

“Invalid use of '\' in replacement text” with non-interactive calls

It seems that replace-regexp has a different behavior if called interactively and not, but I do not understand why, and how to get the wanted behavior. What I want is the non-interactive equivalent ...
1
vote
2answers
70 views

Removing preceding text from each line of a text [duplicate]

I am new to Emacs. I am reading a book on Python. The book has many code examples. What I would like to do is to convert this: 61. def getRandomWord(wordList): 62. # This function returns a ...
3
votes
3answers
1k views

elisp - regex to replace substring with multiple conditions

I want to replace below example line1 ![img](170319_first.png "test") line2 ![img](170319_second.png) line3 ![img](/assets/170319_second.png) to: line1 ![img](/assets/170319_first.png "test") ...
2
votes
1answer
347 views

A compilation-mode conundrum - matching and highlighting

Whilst playing around with compilation-mode for running tests and using HIGHLIGHT faces for failed expectations I've encountered a bit of a conundrum. Question: why does my compilation-errors-regexp-...
2
votes
1answer
129 views

loop (iterate) flush-lines on a list of regex for large files

I have a file (a log) with unique identifiers. I know I can exclude a single unique identifier in emacs using M-x flush-lines. I sometimes need to exclude more than one unique identifier, and for up ...
2
votes
1answer
566 views

help with regexp for compilation-mode (IAR EWARM )

I was wondering if anyone could help me figure out why the compilation-error-regexp for IAR ( compilation-error-regexp-alist-alist, in compile.el ) does not work? The regexp is: (iar "^\"\\(.*\\)\",...
6
votes
3answers
1k views

How to grep marked files in the dired mode of emacs?

I hope it's a simple problem.. I'd like to search for the regexp 'thisWord' only in the marked files of a dired buffer. Usually I used in the dired buffer: M-x grep and then in the mini-buffer grep ...
2
votes
1answer
217 views

Building a list of strings with a regexp while loop

I'd like to perform a regexp search, capture part of a matched string, and save that part to a list. (Eventually the plan is to save this list to a file in a particular format, but for now a simple ...
5
votes
1answer
51 views

Matching categories '\cX' and digit

The regex info material suggests that \\c6 should match digit characters. In UTF-8 encoded buffers it doesn't; some of the other category letters work as expected (l, |, a). Shouldn't \\c6* match ...
3
votes
1answer
33 views

how can i do for replace a part of a regexp

For example i have this text: select * from T1 ; select * from T2 ; select * from T3 ; My objetive is replace the text by this other: select * from T1_H ; select * from T2_H ; select * from ...
3
votes
2answers
318 views

How can I select only the content of a @media in CSS with regexp in Emacs?

I am trying to select the content of @media in CSS with regexp in Emacs (elisp) to make a slight change in the color of the background or font, so I can easily see that I am editing inside of them. I ...
0
votes
1answer
101 views

Why is “*\\Completions\\*” formatting used when specifying buffer names for display-buffer-alist?

From GNU Emacs Manual: 20.6.2: If you prefer Emacs to display a temporary buffer in a different fashion, we recommend to customize the variable display-buffer-alist (see Choosing a Window for ...
8
votes
1answer
946 views

What does a backslash followed by a single quote mean in a regular expression?

Looking at this stackoverflow answer on an old way for specifying the org-file-apps list, I can see that there is a backslash followed by a single-quote character after each file type extension, e.g. ....
0
votes
2answers
147 views

How to write an e-lisp function to extract a sub-string between two given characters (not their positions) in a given string?

I am writing an e-lisp function in my .emacs file to help process Bibtex entries I copy from Google-Scholar. Let's consider this example: @article{jacobsen2011full, title={A full hydro-and ...

1
3 4
5
6 7
9