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
3
votes
1answer
608 views

Regular expression matching group replacement not working

I want to convert .waitForVisible('.someselector') .click(); Into: .waitAndClick('.someselector'); Automatically in a lot of files. So I wrote: (defun hhaamm-replace-regexp-in-buffer (from to) (...
1
vote
1answer
962 views

Programmatically replace all instances of regex with capture groups and sub-matches in buffer

It is clear to me how to programmatically replace all instances of regex in a buffer with some text, something like (pardon my naive code): (defun my/regex-global-replace (regex subst-text) "...
2
votes
1answer
75 views

Match regexp groups or entire text

According to match-string help: Value is nil if NUMth pair didn't match In fact: (string-match "\\(hullo\\)" "hello") (match-string 1 "hello") gives nil Also, a NUM Zero means the entire ...
0
votes
1answer
51 views

Regex replacement of outer symbols

I have a lot of occurances of something =like_this= and I need to replace the both equals signs with this character: ` After looking at this question I thought the following would do it: M-x query-...
1
vote
2answers
71 views

Files for which to activate generic mode

I've created a generic mode for emacs, but I have trouble matching the file names I want the generic mode to be on, right now I'm using proguard-* but I would like to match only file names with ...
5
votes
1answer
192 views

How to use the regexp for digits with an interactive search?

For example, I have the following content in the buffer: 123 1234 12345 123456 1234567 To match the 4 numbers, I need the regular expression of \d{4}. I would like to do the same in Emacs, when I ...
8
votes
1answer
2k views

How to match symbol in regexp?

I'm trying to construct a regular expression that would match symbols, that is, things like function names and the like, according to current major mode settings (it's called syntax table if I ...
0
votes
2answers
50 views

Query replace and commenting out

I want to comment out all environments say myenv in a tex-file. I can find them with the regexp: \\begin{myenv}\(.\|\n\).*?\\end{myenv} (instead of \n I had do type C-q C-j). But what would be the ...
1
vote
2answers
6k views

How do I replace only the first space on a line?

I'm trying to use M-x replace-regex to find and replace the first space on each line. What regex would I use to do this? Everything I've tried either finds no matches or every space. The lines ...
1
vote
1answer
467 views

Regex Quantifier not working in string-match

All the documentation I can find says this should be returning the index of the first of the three x's but instead it returns nil: (string-match "x\{3\}" "string with 3 x's repeating: xxx") update: ...
1
vote
1answer
485 views

why is this trim-space function so complicated/ugly in emacs lisp? [closed]

I just spent at least 10m writing a trim-space function in Emacs lisp: (defun trim-space (string) (let ((regex "\\`[ \n\t\r]*\\(\\(.\\|\n\\)*?\\)[ \n\t\r]*\\'")) (string-match regex string) ...
1
vote
1answer
78 views

Helm would not show line previews if used regexp

When I use regular expressions in helm occur for narrowing, it does not show line previews Is this normal functioning? If so, are there any workarounds? Update: I found out that the error only ...
2
votes
1answer
188 views

How to match more than one instance of a single subexpression?

I'm trying to use a regular expression of the form "foo\\(?:\\(bar\\)\\(baz\\)\\)*\\(quux\\)", but I want the inner two subexpressions to retain all of their matches, not just the first one. I ...
0
votes
1answer
38 views

How to align based on capitalized words?

I have some SQL code in the following style: "CREATE TABLE IF NOT EXISTS test ( \n\ \ id INTEGER PRIMARY KEY, \n\ \ symbol TEXT NOT NULL, \n\ \ date TEXT NOT NULL, \n\ \ val DECIMAL(10, 2) NOT ...
3
votes
1answer
173 views

Why does adding grouping parentheses cause a regexp to no longer match?

If test-string is defined like so (setq test-string "fooXbarXbazXextraYstuff") then the regexp "^[^XY]+X" matches test-string: (string-match "^[^XY]+X" test-string) => 0 (#o0, #x0, ?\C-@) ......
3
votes
2answers
221 views

How to split the output of a `printenv` shell command?

Basically, I want to split a multiline string at the regular expression that, in Perl, would be specified with \n(?=[^\W\d]\w*=). Note that the Perl regexp features a "zero-width lookahead match" ((?=...
5
votes
2answers
13k views

Escaping brackets in regexp

I tried these two, but none of them worked. (if (looking-at "\[") (insert "f")) (if (looking-at "[") (insert "f")) How can I escape square brackets in emacs regexp?
2
votes
1answer
1k views

Is there an equivalent of string-match for regexp?

I'd like to write something like this except to compare the region with a regexp. (if (string-match (buffer-substring-no-properties (point) (mark)) "foo") (bar)) Is there an ...
0
votes
2answers
114 views

Regexp for distinguishing uppercase and lowercase not working

I've made the following command: (defun search-backward-lowercase-alphabet () (interactive) (search-backward-regexp "[a-z]")) But when I run it at the end of a line like the following: abcDEF ...
1
vote
1answer
1k views

Regex to remove spaces inside braces

I have many files with the many of the following forms: export function foo(payload={}) { return { type: bar, payload }; } I would like to do a regex-replace that removes the spaces between the ...
7
votes
2answers
2k views

Only show lines containing phrase/regex

I would like to see all the lines from the current buffer, that contain some phrase, or match regex. Example: My buffer: dam madam madam this is da m a dam 1 dam 2 dam pi dam 321:) is dam 2? ...
0
votes
1answer
33 views

Strip arbitrary characters from a string?

Q: How can I strip non-word characters from a string? For an arbitrary string, how do I strip all characters with non-word syntax? Alternately, using a regexp, how do I strip out the non-alphabetic ...
5
votes
2answers
1k views

in Emacs regex, how to search for uppercase letters only?

I'm trying to build a function but having trouble with the regex. Specifically, I want to match capital letters only. This function is supposed to capitalize words that are in lowercase (the → The) ...
4
votes
1answer
110 views

Regexp replace without interaction

Is it possible to replace regex like query-replace-regex without asking for each matching ?
1
vote
3answers
102 views

Elisp regex matching - evaluate as region vs as individual sexp gives different answers

Here's a chunk of code: (defvar test-string "bla bla bla 10.10.10.10 bla bla bla") (string-match "[[:digit:]]+\\.[[:digit:]]+\\.[[:digit:]]+\\.[[:digit:]]+" test-string) (message (match-string 0 ...
0
votes
4answers
2k views

How to replace multiple newlines for single one in whole file?

I have file which has multiple newlines. I want to receive single newline in their place. I know how to do it in vim(found in google query): :%s/\n\n\n*/\r\r/ How can I do the same in Emacs? ...
0
votes
0answers
112 views

Need help with a snippet

Can someone help me with this snippet? I found it here # -*- mode: snippet -*- # name: jsdoc comment # key: doc # -- /** * $0 `(save-excursion (let* ((pos-fun (re-search-forward "\\<function\\s-*(...
2
votes
1answer
288 views

How to customize files pattern while using rgrep?

elpy-mode internally uses this command to search for a regexp in *.py files. (setq elpy-rgrep-file-pattern "*.py") (rgrep regexp elpy-rgrep-file-pattern default-directory)) However ...
6
votes
3answers
532 views

highlight-regexp in multiple buffers

highlight-regexp is pretty amazing, but when I run it manually by M-x highlight-regexp RET f?o RET 'someface it only applies the highlight to the current buffer. How can I apply a highlight to ...
2
votes
2answers
308 views

Why do we have [:word:] but not [:symbol:]?

I'd like to use [[:symbol:]]+ in a regular expression but this does not exist in Emacs, see ad-hoc section in the manual. Q1: what is the rationale behind this? I'm always happy to understand the ...
1
vote
1answer
533 views

“Args out of range” error when replacing with visual-regexp-steroids

I have been trying to use visual-regexp-steroids without luck (a thread on this package can be found here) When I do vr/replace (C-c p), I get a prompt in the minibuffer. I can enter regexps and ...
6
votes
1answer
54 views

How to check with regular expressions or another way if some package is used in a LaTeX document?

How to check if a specific package is used by a LaTeX document. For example in the below document (intentionally written in an ugly but technically correct format), the packages fontenc, inputenc, ...
1
vote
1answer
93 views

How to non-interactively use regular expressions over more than one line?

Consider a buffer as below. As explained in the documentation . matches any character but newline. Also \n matches newline. So evaluating (re-search-forward "a\(.\|\n\)*b" nil t) should normally have ...
5
votes
2answers
199 views

How to detect if the point is at a closing delimiter?

How to detect if the point is at a closing delimiter? For example: in Elisp: ) in LaTeX: }
2
votes
2answers
91 views

substitute in the filename

In Dired Query and Replace, can we substitute in the filename being visited? For example, I want to substitute "solve" in all *.mod files with the filename *.mod.
5
votes
1answer
305 views

Can occur match phrases that stretch across multiple lines?

Q: (how) can I use occur to match a phrase that stretches over multiple lines? Consider the following buffer: Here's a line of text with the phrase "kittens and puppies". Here's an awkward ...
0
votes
2answers
567 views

How to go to end of match using search-backward-regexp?

I am trying to match a regex using this code (search-backward-regexp "\n[\t\n ]*?\n" nil "NOERROR" 1) search-backward-regexp documenation says Set point to the beginning of the match, and return ...
5
votes
2answers
3k views

Matching multiline comments in regex

I'm writing a major mode for a language that uses (* ... *) as comments. I'm trying to match multi-line comments. e.g. (* foo bar *) I was initially doing "(\\*.*\\*)", however, . doesn't match ...
1
vote
1answer
103 views

search keys and replace by values in another file?

I have a dictionary key-values dict.tsv file like this: key1 value1 key2 value2 key3 value3 And my text.txt file has this: blah blah asdf saf key2 asd blah key1 blah key1 blah key2 blah key3 How ...
3
votes
2answers
2k views

How can I kill a line after a certain character?

Each line in my file has a character ; followed by information that I want to delete. What is the best way to do it? Example: I like phat beatsies; but Mark does not like them I also like dogsies; ...
6
votes
2answers
19k views

How to make a dot match a newline

Often I need to use regular expressions to match strings across multiple lines. In python and other languages, it's possible to ask that dots match newlines (as opposed to the default behavior). Is it ...
9
votes
1answer
1k views

Regex match literal string

I want to write a function (or use an existing one) that will take any string and produce a correctly escaped Regex that matches only that string. What is the fastest and simplest way of doing this?
1
vote
1answer
101 views

Regexp \\text{vars}(\([:ascii:]+\)) -> |\1| doesn't work

Why doesn't this regexp work when I do C-M-%? I need to replace exactly these strings: \text{vars}(\vp) -> |\vp| \text{vars}(\vp') -> |\vp| I don't have anything else of the form \text{vars}*, ...
5
votes
1answer
374 views

replace-regexp: pattern is matching too much

I'm having problems to replace a URL inside a HTML tag with replace-regexp. This is the original string: <img src="http://localhost/~user/foo.bar/place/foo_bar.jpg" ... attr="..." /> This is ...
1
vote
1answer
332 views

Problem in writing regular expression for imenu-generic-expression

I want to use imenu in matlab-mode in emacs. So I add something like (setq imenu-generic-expression '(("Function" "\\(function\\)[\n\t ]*\\[?[ ,_A-Za-z0-9]+\\]?[\n\t ]*=[\n\t ]*\\([_A-...
3
votes
2answers
1k views

How to find (and list) abbreviations in a buffer?

I am using Emacs to write a long thesis in LaTeX. I have no regrets, so far (about the Emacs/LaTeX combination, that is). One of the last things I need to do before I can submit the darn thing is to ...
5
votes
3answers
491 views

how to use CSS to change styling of elements in org-mime emails?

The org-mime page describes how to use org-mime-html-hook to style HTML email by inserting inline CSS. I used this method to successfully change the styling of my body paragraphs by targeting <p>...
4
votes
1answer
66 views

Regexp by Unicode General Category

I'm trying to implement a tokenizer for Haskell, it has a bit involved token structure that can be seen in code here: https://github.com/ghc/ghc/blob/master/compiler/basicTypes/Lexeme.hs In regexp-...
1
vote
1answer
74 views

Regexp doesn't work

I want to convert all the deques in my python code to lists. I.e. deque([1]) should become [1]. I tried M-% deque(\(.+\)) RET \1 RET, but it said Replaced 0 occurences. What's wrong with my regexp?
7
votes
2answers
300 views

When to use normal incremental search in place of the regex version?

Assuming familiarity with regex, is there ever any reason to use the normal incremental search (C-s) over regex (C-M-s)?