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
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 ...
0
votes
1answer
191 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
51 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
99 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 ...
2
votes
1answer
279 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 ...
0
votes
0answers
49 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
251 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-...
1
vote
1answer
395 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
64 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
651 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
251 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
119 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
437 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
758 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
137 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
46 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
144 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
70 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
820 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
78 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 ...
4
votes
1answer
224 views

Equivalents to vim \zs and \ze?

In vim, all instances of an apple tree can be replaced by an orange tree with :%s/an \zsapple\ze tree/orange/g. \zs and \ze mark the start and end of the passage to be operated on. Is there something ...
2
votes
1answer
61 views

Better way to find regexp in emacs buffer

I'm trying to make a function to read a whole buffer and find lines that match with regexp, I think to read the whole buffer and save to list of strings but I read that it's very inefficient. Whats ...
1
vote
1answer
1k views

How do I match a closing bracket in Emacs lisp

I would think this was really simple. I am trying to match closing delimiters in Emacs Lisp regex. I can match {}()[ no problem, but I cannot match ]. I have been using re-builder and have tried "[\\}\...
8
votes
1answer
320 views

Most performant matching of “any char”

On https://www.emacswiki.org/emacs/MultilineRegexp one finds the hint to use [\0-\377[:nonascii:]]*\n instead of the standard .*\n to match any character up to a newline to avoid stack ...
2
votes
2answers
204 views

query-replace-regexp on specific lines

how does one do a query-replace-regexp to add a prefix string but not on EVERY line but rather just lines with text (or start with specific text?) i know how to add a prefix with query-replace-regexp ...
0
votes
1answer
426 views

How to match the URL component from a markdown link?

I've been trying to hide the URL component of the markdown links in my buffer. I could select the markdown links using "\\(\\[.+\\]\\)\\((.+)\\)" However, I just want to capture the second half (...
1
vote
2answers
173 views

Preventing font-lock string match from going past end of line

I have a custom mode to highlight my log files. In it, I attempt to highlight strings that are surrounded by double quotes("[^"]*"). In the files, there are sometimes lines with an odd number of ...
0
votes
0answers
165 views

Match regexp group

I am trying to modify org-wikinodes.el to match any org-mode headline (not just camelCase text. So I have replaced the default regular expression with: *\\s-\\(.+\\) This works. But in order to link ...
0
votes
1answer
141 views

Custom search function for dictionary lookup

I have a plain text dictionary in which each headword is matched by ^# \(† \)?headword, How can I make a custom search function that relieves me of typing the whole regex every time I want to look ...
2
votes
1answer
70 views

Constructing an advice around org-html--wrap-image

This is a follow-up question from a previous thread The function org-html--wrap-image adds a <p> tag around its contents, as you can see here: ELISP> (org-html--wrap-image "bla" t) "\n<...
2
votes
2answers
376 views

How can I get regexp-opt (or similar) not apply on the word with added underscore?

See excerpt below for code used in emacs Cadence SKILL mode: (cons (concat "\\<" (regexp-opt '("nil" "t") t) "\\>") 'font-lock-constant-face) it does a brilliant job at highlighting nil and ...
9
votes
1answer
2k views

Evil mode and regular expressions

In Vim, if I want search for all variable names LX where X is a number, I can simply type /L\d. However, this doesn't seem to work with evil. Is there another syntax for regular expressions in Evil ...
2
votes
1answer
364 views

Difficulty with query-replace-regexp

I'm having trouble getting query-regex-replace to do what I want. I've read the docs several times and still can't figure it out. Here are a few examples of things I can't figure out how to do with ...
4
votes
2answers
666 views

How can I add a compilation error regex for node js?

I can't seem to get emacs to recognize node stacktraces. As you can see, I've added what I think is a valid regular expression to compilation-error-regex-alist and compilation-error-regex-alist-alist, ...
0
votes
1answer
52 views

replace all '*' with spaces EXCLUDING org headers?

I converted a file to org-mode, but have a lot of extra asterisks. I want to replace each * with a space, but I want to exclude the * in a header line. How do I do this?
1
vote
1answer
450 views

Syntax highlighting: highlight text followed by a pattern, but not the pattern itself

I'm writing a major mode to handle syntax highlighting for a language that allows namespaces just like in C++ e.g. somewhere::out_there. I'd like to highlight the text somewhere, but leave the ...
4
votes
1answer
146 views

replace-regexp-in-string stops replacement with “^.”?

I've encountered a wierd situation when trying to match the first character of a string using a regex with alternatives. It seems that replacement stops when matching a single character using a ...
1
vote
0answers
69 views

Good way to match symmetric delimiters

I am trying to match $$ resp $ pairs to switch into latex-mode via polymode in a markdown file. $$ is the delimiter for displayed math in this case and $ for inline math. I know that this is not a ...
1
vote
1answer
124 views

Sort Dewey Decimal list?

If I have a list of books' call numbers in Dewey Decimal in a buffer, is this something I could sort with Emacs regex -- or is this an elisp task? For example, 261.1 C87 comes before 261.104 Z99 ...
3
votes
1answer
1k views

How do I lowercase (downcase) a group with replace-regexp?

I have some text that looks like this: "CFName" "CLName" "CDOB" "CGender" I would like to get output where are of the text inside the double quotes is lowercased, like this: "cfname" "clname" "cdob"...
4
votes
1answer
833 views

regexp to find two consecutive and identical words not necessarily in the same line

According to https://www.emacswiki.org/emacs/RegularExpression#regexp \(\w+\) +\1\> finds two consecutive identical words. It works if the words are in the same line separated by an ...
1
vote
1answer
210 views

replace-regexp-in-string not replacing the entire match

So, I have a function that sends a URL to bit.ly for shortening and returns the short URL, and a (replace-regexp-in-string), which is supposed to grab the URL out of a string to passes it to that ...
2
votes
1answer
141 views

align (indent) in bibtex file

I have a bibtex file, and would like to have indentation as follows. @book{moon70:trees, AUTHOR = {John W. Moon}, TITLE = {Counting labelled trees}, YEAR = 1970, ...
0
votes
1answer
227 views

Left-align org-mode like FoldingText… Regexp?

I posted this question on stackoverflow earlier, but perhaps this is a better place for it? I am looking for a way to left-align text in Org-mode so that the paragraphs line up like in FoldingText. ...
0
votes
1answer
1k views

How to match this string with elisp regexp?

I'm writing an extension to open a custom link type in Org-mode with Kiwix URL. (Kiwix is an offline Wikipedia program). Here is my kiwix.el source code. I created a wiki: link type for Org-mode. ...
2
votes
1answer
118 views

Problem with replace-regexp - Invalid use of \ in replacement

I want to add extra expressions to some JS code, but taking care of the correct element ID (in the example below, the ID is scatter). document.getElementById("scatter").height="400"; --> document....
1
vote
1answer
53 views

Using reference in replace-regexp

What should I type in replace-regexp command to change the first list below into the second? I'd like to repeat each number at the beginning of line. To achieve that, I assume that I'll have to refer ...
0
votes
1answer
92 views

a function to clear out particular words/characters via regex

I have a specific request to all you regex wizards :) I want to create a simple function that deletes or cleans out several parts of a text file based on predefined keywords (such as single capital ...
0
votes
2answers
666 views

elisp trying to double backslashes in windows pathname - concat strips a slash escape

I'm trying to insert a string pathname into a buffer on a Windows machine. The variable holding the string has the right number of backslashes (two) in the directory pathname. I know this because when ...