Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regexps in Elisp to include newlines

Tags:

regex

emacs

elisp

I'm trying to add a special markup to Python documentation strings in emacs (python-mode).

Currently I'm able to extract a single line with:

(font-lock-add-keywords
 'python-mode
 '(("\\(\"\\{3\\}\\.+\"\\{3\\}\\)"
    1 font-lock-doc-face prepend)))

This works now:

"""Foo"""

But as soon there is a newline like:

"""
Foo

"""

It doesn't work anymore. This is logical, since . doesn't include newlines (\n). Should I use a character class?

How can I correct this regular expression to include everything between """ """?

Thanks in advance!

like image 966
wunki Avatar asked Nov 26 '08 12:11

wunki


People also ask

Does \s match newlines?

According to regex101.com \s : Matches any space, tab or newline character.

How do I find special characters in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).


1 Answers

"\\(\"\\{3\\}\\(.*\n?\\)*?\"\\{3\\}\\)"

The "*?" construct is the non-greedy version of "*".

like image 95
huaiyuan Avatar answered Oct 07 '22 04:10

huaiyuan