Skip to main content
Share Your Experience: Take the 2024 Developer Survey

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
0 answers
17 views

How to grab previous word from buffer in custom company-mode backend?

I want to make my own backend for company-mode. It must be context sensitive: completion list should be build based on previous word in editor. When I write: git res|, list should contain word "...
BPS's user avatar
  • 113
0 votes
1 answer
27 views

Paragraphs in regexps that are not defined in the minibuffer

I sometimes have epubs that I prefer reading in org-mode, so I convert them into text files and format them while reading. That leaves all the footnotes as bare numbers, so I used replace-regexp to ...
Alexander Praehauser's user avatar
0 votes
0 answers
46 views

replace-regexp-in-string inserting rather than replacing at some matches

This is my first question. My searches through answers to (some of the) similar questions hints that I likely have to educate myself on Match Data, but I'm asking here now anyway. Anyway, I have a ...
ramsay's user avatar
  • 1
0 votes
2 answers
31 views

re-search-backwards find first Uppercase letter

I want to find the first occurrence of an uppercase letter. I am trying to use re-search-backwards; yet it is not working as intended. Here's an example: (defun test () (interactive) (re-...
Peter Petigru's user avatar
0 votes
0 answers
14 views

counsel-M-x search for command names optionally beginning with my:

Goal Use counsel-M-x to browse commands that begin with a particular character, except that they may be optionally prefixed by my: Example counsel-M-x >> ^org matches commands beginning with org ...
user615536's user avatar
0 votes
1 answer
32 views

Show text between two lines

Suppose I have the next log file: 2023-09-17 00:59:00.245 Start ... here many lines of text#1 (about 1000 lines of text) 2024-02-23 18:48:06.692 INFO [MyClass:myExecutor-3] Some process - enter to ...
a_subscriber's user avatar
  • 4,124
0 votes
1 answer
45 views

Is there an efficient way to fontify matches of thousands of different strings in a buffer?

I have a list of thousands of different strings made up of different numbers of words (e.g. names of specific people) that I would like to fontify in a buffer. Some applications I could use for ...
John Kitchin's user avatar
  • 11.9k
0 votes
1 answer
36 views

How to regex replace a string with matches in purely scripting, not reading from buffer

I'm a beginner in Emacs and Elisp, but do have programming experience. My question is quite straightforward, I want to have a command open-git that when in a git directory, it opens the url inside a ...
Stroopwafel's user avatar
9 votes
1 answer
595 views

How does Emacs compile the regular expressions?

I'm writing code that will have to do a lot of regular expression matching. I understand that a function like string-match or re-search-forward needs to process its regexp argument given as a string ...
phs's user avatar
  • 1,239
1 vote
1 answer
34 views

Why can't my rx-let bindings include `bol` or `line-start`?

The following works: (rx-let ((headers (? (or "From" "To" "Cc") ":"))) (list "mail.amazon" (rx bol headers anything "amazon" anything) ...
Webdev Tory's user avatar
0 votes
1 answer
52 views

Replacing in nested regexps

I'm having to replace the configurations of keymaps in my use-package with define-key expressions and thought I'd use that to get more proficient with using regexp-replace. The situation is this: (:...
Alexander Praehauser's user avatar
0 votes
1 answer
65 views

Are Lookaround Zero-Length Assertions added to Emacs regular expressions meanwhile?

While browsing I've bumped into a mindblowingly useful tool when it comes to regular expressions: Lookaround Zero-Length Assertions - https://www.regular-expressions.info/lookaround.html . It's that ...
starquake's user avatar
0 votes
1 answer
51 views

How to search regex in a url-retrieve buffer?

I'm trying to implement a login system with emacs lisp, the login system has a hidden input field that's initially empty and is filled by a piece of javascript code when submitting. I'm currently ...
ygy's user avatar
  • 103
0 votes
0 answers
68 views

Use different font for math fragments in org mode

How do I use the fixed-pitch font for math fragments in org-mode buffers? I.e., I want to use a monospace font for all strings of the form \(...\) or \[...\], where ... may include newline characters ...
A is for Ambition's user avatar
0 votes
2 answers
63 views

How can a regex replace that computes the replacement in a function using back references be converted to a CLI batch version processing line by line?

An example of this is a script to output the contents of .zsh_history file in the manner of the history command, so an emacs script would be invoked by a command line like display.sh.el < ~/....
vfclists's user avatar
  • 1,423
0 votes
1 answer
140 views

Am I using "replace-regexp-in-string" the right way?

I want to view ~/.zsh_history files the way they are displayed by the history command with raw unixtimestamps converted to properly formatted dates. : 1568128379:0;cp -a ~/.zshrc.pre-oh-my-zsh ~/....
vfclists's user avatar
  • 1,423
0 votes
1 answer
36 views

In an Elisp regexp, does anchor `^` not work with new line in front?

In an Elisp regexp, when I want to specify a pattern across two consecutive lines, I found that ^J^.* (^J is new line, and ^ is anchor of the beginning of a line) doesn't work, but ^J.* works. I was ...
Tim's user avatar
  • 5,027
0 votes
0 answers
44 views

Regexp searches that repeatedly cover expressions

I'm just going through an old Latex-text I've written and wanted to use regexp-replace to make any pair of brackets containing at least one other pair of brackets into a \left(.*\right)-expression. ...
Alexander Praehauser's user avatar
0 votes
1 answer
26 views

Define new comment style with string literals rather than characters

I am trying to define another comment style, similar to what we can do with modify-syntax-entry: (modify-syntax-entry ?/ ". 124b") (modify-syntax-entry ?* ". 23") (modify-syntax-...
Jake Ireland's user avatar
1 vote
1 answer
62 views

How to add numbers to all lines matching a certain regex?

I have a Markdown file like this: # Title ## My Theme Pellentesque dapibus suscipit ligula. ### Mine is yours Donec posuere augue in quam. ## Your Theme * Suspendisse potenti. * Aenean in sem ac ...
halloleo's user avatar
  • 1,345
0 votes
1 answer
39 views

Is the '$' (end of line anchor) somehow concurrent with the '\n' (newline)? Please explain these results

In normal regex, I would expect the following to be true: (should (equal (string-match "^abc$\nx" "abc\nx ") 0)) ;; nope! nil (should (equal (string-match "^abc\n$x" &...
Jared's user avatar
  • 101
1 vote
1 answer
139 views

replace nth-match of a regexp in a string

I'm looking for a function that can replace the nth-match in a string. Here's an example. 2 is the index of the nth-match, [0-9]+ is the regular expression to search. foo is the replacement and "...
rdrg109's user avatar
  • 428
1 vote
1 answer
41 views

function needed to replace short, static list of keywords

In my workflow I copy emails into an orgmode file. I'd like to have head of the email formatted. In english emails (just as an explanation) I'd like to have *To:* instead of To:, because orgmode then ...
Keks Dose's user avatar
  • 508
1 vote
2 answers
72 views

How can I make occur display only first line of match?

The context for this is that I was trying to write a function to help me write LaTeX, specifically I'd like to list all the future references, i.e. when you use a \ref{<something>} before \label{...
Alessandro Bertulli's user avatar
0 votes
1 answer
29 views

Strange behavior with replace-regexp

I have the following line: #ifdef DCC_3D_HPP || DCC_2D_HPP I want to replace it with: #if defined(DCC_3D_HPP) || defined(DCC_2D_HPP) I use the following prompt with replace-regexp: #ifdef \([A-Z0-9_]+\...
daedsidog's user avatar
  • 157
1 vote
0 answers
47 views

How can I search for CamelCase words in a buffer using Elisp?

How can I find CamelCase words in the buffer using Elisp? Have started with the following but the pattern does not cover all situations of CamelCase. (defun detect-camel-case-words (string) "...
Dilna's user avatar
  • 1
0 votes
1 answer
67 views

Is there a literal-string version of "looking-at"?

The function looking-at matches a regex, in some cases however I want to match against a literal string. While (looking-at (regexp-quote text)) works, it seems like there might be a way to do this ...
ideasman42's user avatar
  • 8,826
0 votes
1 answer
59 views

build regexp in lisp [duplicate]

Is there a package that provides a clearer syntax for building complicated regexes in code? I currently have in my .emacs this regex, which matches various Java package names: "\\(com\\.example\\|...
Troy Daniels's user avatar
0 votes
3 answers
117 views

How to delete all the files matching regular expression in elisp?

I want to make some clean-up with auto-generated files. How can I delete all the files matching some regexp mask from within elisp? As an example of desired functionality... Something like the line ...
martinoidar's user avatar
0 votes
1 answer
45 views

Multiple regexp replace in string

I need to search for every declared variable in a string with format %^{var}, prompt a value and replace in place. (let ((primitive "(%^{size}, %^{center})")) (if (string-match "%^{\\(...
my_display_name's user avatar
0 votes
1 answer
25 views

Regexp for first consecutive non whitespace ending with @?

I have a mailing list whose lines contain garbage characterized by spaces, like this: bar qux [email protected] for which I want to extract [email protected]. I tried this unsuccessfully: M-x query-replace-...
user avatar
1 vote
1 answer
33 views

Trouble maintaining buffer position when using re-builder

I am having trouble maintaining my current location in a document while running the re-builder command in Emacs. In a long document the buffer moves to the top of the document whenever I run the re-...
Edman's user avatar
  • 1,229
0 votes
1 answer
44 views

isearch-forward-regex does not find a regex found by re-builder

I wanted to create a regex to find these dates: 1966/08/20 2023/02/12 In re-builder I was able to build this: "\([0-9]\{4\}\)/\([0-9]\{2\}\)/\([0-9]\{2\}\)" In the buffer the dates are ...
Edman's user avatar
  • 1,229
0 votes
1 answer
123 views

How to use regular expressions with helm-projectile-grep?

I would like to find all strings in files matching the pattern foo_*_bar. I know that one can use \s to match empty characters. What's the pattern for zero or more characters? Could not find it by ...
honey_badger's user avatar
1 vote
2 answers
100 views

How to align a region on &?

I thought M-x align-regexp & 1 1 y on the region containing &'s would align all columns, but it does not, so I must be using it wrong. What is the correct way? Reminder: man says white spaces ...
user avatar
0 votes
2 answers
70 views

Is it possible to refile using regexp applied to some property?

If I do M-x org-refile on qux using the first (commented out) version of org-refile-targets, it brings up Foo, as expected. Using the second version, I get "No refile targets", whereas I ...
user avatar
0 votes
0 answers
36 views

How to align source code section in both Org-src-edit-code, Org-mode, and exported PDF file?

How to configure emacs such that code (in this case :which-key) is aligned in both org-edit-src-code org-mode and exported PDF? I'm guessing this is somewhat related to tabs getting different amount ...
Garid's user avatar
  • 641
2 votes
2 answers
70 views

Colourize buffer names differently for the files located in different directories

I am working upon 2 projects that have similar structure. And sometimes occasionally modify a file in the wrong buffer. So I desided to set different colours for the files in the different projects: (...
user4035's user avatar
  • 1,069
0 votes
1 answer
78 views

How to set `org-agenda-files` depending current date in order to lessen number of files to load?

I use org-roam-dailies for my TODO lists for given day. And currently, I set my org-agenda-files to org-roam-dailies's directory as below: (setq org-agenda-files '("~/roamnotes/daily/")) ...
Garid's user avatar
  • 641
0 votes
2 answers
202 views

Regex to find and replace a specific character inside of multiple quoted strings in a string

Given an example string of JSON (formatted here for reading, actual input may or may not be formatted): { "page":1, "per_page":6, "total":12, "...
David's user avatar
  • 126
0 votes
1 answer
29 views

searching for <beginningOfLine><Comma>

how can I (interactive) search for <beginningOfLine>, I tried ^ which means beginning of line in perl Also \A which does signified the same in ruby. C-s ^, C-s \A, I also tried search-forward-...
american-ninja-warrior's user avatar
1 vote
1 answer
165 views

What is the replace regexp string for applying 3 digit grouping to any number?

the title gives it all away: I want this 9874599842006432.08 To become this 9,874,599,842,006,432.08 with 1 single replace regexp operation and flexible, so that the same string also would work on a ...
starquake's user avatar
2 votes
3 answers
60 views

How to programatically determine which alternative of a regular expression is responsible for a match?

Suppose I have a regular expression with many alternatives, such as "[a-z]+\\|[A-Z]+\\|[0-9]+" and it has just matched some text. I'd then like to know which of the given alternatives ...
Ruy's user avatar
  • 839
0 votes
1 answer
53 views

Why do Emacs Lisp regular expressions match the percentage and dollar sign as word characters?

I ran into this behavior by accident and it seems quite unusual to me. Why do Emacs Lisp regular expressions match the percentage and dollar sign as word characters? One theory I had was that this is ...
fap's user avatar
  • 141
2 votes
1 answer
105 views

Why "looking-back" does not match "[ \n]+"?

I want to do this replacement: \includegraphics{file } ==> \includegraphics{file} I wrote this code: (goto-char (point-min)) (while (re-search-forward "\\\\includegraphics{" nil ...
Onner Irotsab's user avatar
0 votes
1 answer
38 views

"-o" like options for `elgrep`?

Sometimes I just want the matched result by regexp instead of the whole line, this seems possible via the -o option in grep I discovered elgrep in emacs recently, which is fantastic! Any chance we can ...
davyjones's user avatar
  • 127
0 votes
0 answers
26 views

Change foreground colour or files showing error in rsync log file

I want to colour the file names that are logged with an error when using rsync. One such file will be as follows 2022/12/07 21:33:49 [3672] WARNING: b2-infocs/unix-like/2006-hagan-gcc.pdf failed ...
Dilna's user avatar
  • 1
0 votes
1 answer
50 views

Using general comment character used in programming languages

I am using highlight-regexp with a regexp pattern that includes the elisp comment characters ;;. How can I extend the pattern so that it adequately handle the general comment character for each ...
Dilna's user avatar
  • 1
1 vote
1 answer
84 views

Using a sub-expression for `highlight-regexp`

I have made a face to highlight elisp comments starting with ;; [ using hi-lock. My intention is to only colour the expression [text] using SUBEXP. (highlight-regexp "^;; \\[.+\\].+$" 'face-...
Dilna's user avatar
  • 1
1 vote
1 answer
12 views

Switch a TeX \macro and a script on a region of text

How can I search and replace in a region of a (TeX)buffer all occurences of \macro{foo}_{bar} to \macro{foo_{bar}}, foo and bar not being "constants" ?
cjorssen's user avatar
  • 197

1
2 3 4 5
10