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.
383
questions
0
votes
0answers
9 views
remove erroneous commands from ESS transcript
I'd like to better use my ESS transcript file while working on R.
I know how to clean the .Rout file only keeping the command lines.
But what could be great is to remove all the command lines that ...
0
votes
1answer
47 views
replace-regexp-in-string: How to prepend a string to the entire string matched?
I am adding divider lines to certain sections of the reports generated by the binary ledger [ https://www.ledger-cli.org/ ] through the function ledger-do-report in the ledger-mode Lisp library [ ...
1
vote
1answer
53 views
How can we get input using emacs?
In order to compile my lua code i use this config
(defun lua-main-compile ()
"use lua to compile my lua code"
(interactive)
(compile "lua main.lua"))
In my lua code ...
1
vote
1answer
39 views
How can I get the search string from incremental regexp search to use with replace regexp
If I want to do a replace-regexp I usually start with a isearch-forward-regexp so I can get immediate visual feedback on the search string.
From there I'd like to grab the search string for using in ...
1
vote
1answer
44 views
Regular expressions tool example explanation
Reading the section regular expressions in EmacsWiki appears this:
You can use a tool to construct regexps. For example, you can use ‘rx’ like this:
(rx (or (and "\*" (*? anything) "*/&...
0
votes
0answers
17 views
What regexp gives this Imenu output?
Suppose I have the following Python script:
def foo(x) -> int:
y = 2
return y+x
class Bar:
def __init__(s):
self.s = s
def buz(x, y):
return x*y
My current Imenu ...
0
votes
1answer
41 views
Regex to replace subset of repeated pattern
ABC、DEF, GHI, JKL、MNO
PQR、STU, VWX、YZ
How do I replace all , s with 、 in the repeated pattern above?
The repeated pattern can be mapped with the regex below:
"^\\([A-Z]+、\\)\\(\\([A-Z]+\\), \\)+&...
0
votes
1answer
28 views
How can I paste a defvar into the minibuffer?
I've written a regular expression to transform Markdown links into Org Mode links and I've stored it into a variable.
(defvar markdown-link-to-org-regexp "s/\[\(.+\)\](\(.+\))/[[\2][\1]]")
...
4
votes
1answer
151 views
Regex for the last line of a string
I'm trying to search for the last line in a string (lines separated by \n) using string match but I cannot make it work as intended. The code below evaluates to 0 while I would have expected 4. I ...
1
vote
2answers
28 views
Remove everything except match of regexp?
I have a file with a structure like:
some text and numbers and a final number: 1.213,31
some other number 1231 and text
again some text and a final number: 53,00
something else
again a final number: ...
1
vote
1answer
21 views
Interactively pass argument and use regex in find replace
I have written the following code
(defun krmet-replace()
(interactive)
(save-excursion
(save-restriction
;; Narrow to the region
(narrow-to-...
1
vote
1answer
66 views
org-mode: specifying a hyperlink target as regex and jumping directly to match?
The documentation of a Fortran code, being done in org-mode, is intended to
be done in such a way that changing the Fortran source code to be documented
should be kept at a minimum.
There is the ...
1
vote
1answer
59 views
Emacs outline-mode custom heading and space between headers
I'm trying to define a structure for outline-minor-mode so I can organize my init.el conf without relying in org.
So far I could not get to work with two levels of heading, I would like to have ;;; ...
1
vote
1answer
23 views
Syntax highlighting of keywords only if following builtin
I'm writing a syntax highlighting mode and am trying to highlight certain arguments, only if they are following a specific word.
I've written a MWE to show my problem:
(defvar mwe-builtin-list '("...
1
vote
1answer
23 views
Decode double-back-slashes-prefixed octal codes in 'utf-8
I have a string "\\303\\273" that I want to decode into û.
It's known that (decode-coding-string "\303\273" 'utf-8) evals to û.
However, I'm not able to translate "\\303\\273&...
1
vote
1answer
25 views
flush-lines fails in script but works when called otherwise
I'm very new to programming in Elisp so maybe the mistake is there but I'm completely stuck right now.
I wanted to make a small, simple Elisp function to remove the AUTOs from my Verilog files so I ...
1
vote
1answer
35 views
Remove multiple blank lines in a buffer, leave one
I'd like to take a buffer which looks like this:
one
two
three
four
five
And turn it into this
one
two
three
four
five
I've tried using delete-blank-line, however, it will only effect the area ...
0
votes
1answer
33 views
.+$ fails to match remainder of line
This matches nothing:
Query replace regexp (default \.org.+ → ): \.org.+$ →
The same without '$' matches incompletely:
I'd like to be able to match \.org and all that follows.
0
votes
2answers
67 views
Filter list by regexps
How can I get all elements of a list which don't match multiple
patterns? The patterns are given as a list.
For instance, I want all files without '("no" "nix") in them.
(setq se-...
1
vote
0answers
28 views
`string-match' and `match-string' in Emacs Lisp [duplicate]
I try to understand the functions match-string and string-match in Emacs Lisp.
The manual provides what seems to be (?) a reproducible example:
(string-match "\\(qu\\)\\(ick\\)"
...
1
vote
3answers
61 views
imenu to copy the target instead of jumping to it?
Is it possible to use imenu (or another package) to copy the target (specified by a regex) rather than jumping to it?
This is an effort for tackling the question: How to list or autocomplete pandoc-...
0
votes
0answers
42 views
Ignoring specific files in recentf-mode by their exact filenames
I am having trouble to Ignoring specific files in recentf-mode.
(add-to-list 'recentf-exclude "company-statistics-cache.el")
On my end, I want to ignore them by their exact file names instead of ...
0
votes
1answer
40 views
Programmatically find-and-replace custom sequence
I need to write a command which will apply to my buffer custom, predefined copy-and-replace sequence.
What I have is:
(defun custom-processor ()
"Formats logs to human-readable format"
((replace-...
0
votes
1answer
94 views
How to search with a regexp before point?
I would like to know a function that searches a pattern located before point.
re-search-backward is mentioned in the Elisp manual but I do not understand its purpose. Indeed, I do not understand the ...
0
votes
1answer
41 views
Regexp: how to search-replace any html attribute in current buffer
Disclaimer: I'm still trying to learn elisp so I have not gotten yet to the RegExp section of the Elisp intro or Emacs manual.
I would like to know how to search and replace an HTML buffer that has ...
0
votes
1answer
40 views
thing-at-point does not bound url properly due to backslash in url
I am trying to filter URLs and elisp thing-at-point 'url doesn't bounds a url properly because it has backslashes \in it. I believe that backslashes are unsafe in urls, but that's a separate topic. ...
2
votes
1answer
41 views
Why is the order of : and - important in a regexp character class
Defining the following function
(defun windows-path-backwards ()
(interactive)
(skip-chars-backward "a-zA-Z0-9\\\\_-:"))
and running it while standing at | in the below text
C:\this\path|
...
2
votes
2answers
70 views
Not matching a character at the beginning of line
I would like to match lines that have the phrase a test but not start with >. So I want to match
This is a test
a test this is
but not
> This is a test
I am not sure how to do this without ...
1
vote
1answer
56 views
How to replace strings including `.` (dot) as first character using dired-do-find-regexp-and-replace
(defun my-find-orgfiles ()
(interactive)
(find-name-dired "." "*py"))
(global-set-key (kbd "M-n") 'my-find-orgfiles)
Than, t to toggle marked/unmarked files (thus marking them all, since none ...
1
vote
1answer
63 views
search-forward-regexp with back reference
I have the following text
\begin{comment}
TEXT
\end{comment}
\begin{verbatim}
TEXT
\end{verbatim}
and the regular expression "\\begin{\([^}]*\)} to match the \begin statements and capture the ...
2
votes
0answers
47 views
org-table: hide grid lines
I like how org-mode allows me to quickly create and edit tables. However, I do not like the table's appearance and I would like to modify it.
Customizing the face only lets me change foreground and ...
1
vote
2answers
105 views
How to get the position after matching and replacing a regular expression (replace-match)?
SHORT
How to substitute (nicely) a regular expression by a string in pure elisp, like the unix command sed.
LONG
I'm using the function replace-match after string-match, but I can't get the right ...
1
vote
1answer
192 views
How to regex match words? (in multiple languages)
I have a simple regex search which matches [A-Za-z], however I would like it to match other characters which would be used in dictionary words for non-english languages. e.g. ü, Ø, ɵ 人参, while ...
3
votes
2answers
96 views
Check whether a string is a date in a specific format, e.g. “%Y-%m-%d”
I have a string and would like to check whether it contains a date in a given date format, e.g. "%Y-%m-%d", with format as for format-time-string. This is for a package and users can configure the ...
0
votes
1answer
48 views
How to set a variable that matches different type of strings?
I have these strings:
First Type:
arXiv:1234.56789
arXiv:1234.56789v1
Second type:
hep-th/123456789
hep-th.AB/12345689
hep-th/123456789v1
hep-th.AB/12345689
Belong to the second type even strings ...
3
votes
1answer
53 views
can emacs memoize regexps?
This is really a question about implementation rather than anything a user of GNU Emacs would be interested in.
Emacs regexps are represented as strings. There is a nice rx DSL for constructing ...
4
votes
1answer
128 views
Non-greedy regex group
This is related to my previous question over here.
(replace-regexp "\\(\\[\\[\\)\\(zotero:.+\\]\\)\\(\\[.+,[《〈][^《〈]+[》〉]\\)?.+。\\(\\]\\]\\)" "\\1cite-\\2\\3\\4,頁")
I'd like to use the above code ...
1
vote
2answers
76 views
How to match first character in line after asteriks and space using regex?
For example i have lines:
* test1
** another tests
* some other line
I would like to match t in first line, a in second, s in third.
0
votes
1answer
59 views
what does the vertical bar “|” mean in sentence-end?
Reference to 12.1 The Regular Expression for sentence-end of Elisp introduction
It illustrate alternatives of sentence-end as
\\($\\| \\| \\)
^ ^^
TAB SPC
and explains:
Two ...
0
votes
0answers
56 views
Why is Emacs hanging or seeming like it isin an infinite loop?
In my current Emacs major-mode for Tecplot macro development, I have a few functions defined:
(defconst tecplot-macro-mode-syntax-table
(let ((table (make-syntax-table)))
;; ' is a string ...
0
votes
1answer
44 views
How do I build a regexp that enables Emacs to set all words to the left of an equal sign a specific font-lock-*-face?
Following some help provided by Tobias, I am trying to assign a font-lock-keyword-face to all text (:alpha:) to the left of an equal sign (=) in a new Tecplot major mode. I read through the regexp ...
0
votes
1answer
114 views
Regexp for the inverse of a match
Given a regexp A, how do I derive the inverse regexp B such that given any text exactly one of the two regexes will match it.
For example in the below example:
(rx (or (group ---regexpA---) ; a ...
1
vote
1answer
306 views
How to regexp-search for a word surrounded by spaces
I'm looking at this
One use of regexp-quote is to combine an exact string match with
context described as a regular expression. For example, this searches
for the string that is the value of ...
2
votes
1answer
55 views
Query replace regexp with a distant match?
I have a single transcript file for a set of course lectures I am watching. After doing some formatting on the file. I have it broken up into sets of lessons where the main heading is the lesson title ...
0
votes
1answer
52 views
Limit font-lock highliting to first N chars of line
I write regex for font-lock and I know that the date should appear in first 40 chars of line.
How can I limit search beyond some limit?
I tried to add ^.\\{0,40\\}XXX but Emacs regex . is greedy ...
0
votes
1answer
54 views
Regular expression to ignore a domain in address completion
I read the manual on address autocompletion:
mu4e-compose-complete-ignore-address-regexp — a regular expression to filter out other ‘junk’ e-mail addresses; defaults to "no-?reply".
I am looking ...
-1
votes
1answer
138 views
Counting number of matches in number of lines for regex search
I have functions that run re-search-forward and gather its output into a buffer.
(defun my-search (pattern)
"Uses `re-search-forward` to return matching lines."
(let ((src)
(rslt))
(...
0
votes
1answer
131 views
re-search-forward Regex behavior for OR boolean
We usually use \| to depict OR in Regex. Using a\|b as input to Regexp I-search and Occur both gave the desired result. But re-search-forward seems unable to recognize this notation.
abcdcba
(re-...
2
votes
2answers
197 views
Regexp for matching quoted strings that may have control characters in them
I am trying to use a regexp to match strings, e.g. a pattern that matches these kinds of quoted strings:
"test" or 'test' or 'test with "quote"' or "test with 'quote'"
This pattern matches all those,...
0
votes
1answer
38 views
How to find the next match with re-builder?
I built a complicated regular expression with re-builder and see that it works with the matches I want. How can I find all the matches, as I would with C-s in isearch-forward?