Regexp in Emacs

Cette partie est copiée de https://www.emacswiki.org/emacs/RegularExpression

Between brackets [], the following are special : ] - ^

.        any character (but newline)
*        previous character or group, repeated 0 or more time
+        previous character or group, repeated 1 or more time
?        previous character or group, repeated 0 or 1 time
^        start of line
$        end of line
[...]    any character between brackets
[^..]    any character not in the brackets
[a-z]    any character between a and z
\        prevents interpretation of following special char
\|       or
\w       word constituent
\b       word boundary
\sc      character with c syntax (e.g. \s- for whitespace char)
\( \)    start/end of group
\< \>    start/end of word (faulty rendering: backslash + less-than and backslash + greater-than)
\_< \_>  start/end of symbol
\` \'    start/end of buffer/string
\1       string matched by the first group
\n       string matched by the nth group
\{3\}    previous character or group, repeated 3 times
\{3,\}   previous character or group, repeated 3 or more times
\{3,6\}  previous character or group, repeated 3 to 6 times
\=       match succeeds if it is located at point

Here are some syntax classes, also known as character classes, that can be used between brackets, e.g. [[:upper:]\|[:digit:]\.].

[:digit:]  a digit, same as [0-9]
[:alpha:]  a letter (an alphabetic character)
[:alnum:]  a letter or a digit (an alphanumeric character)
[:upper:]  a letter in uppercase
[:lower:]  a letter in lowercase
[:graph:]  a visible character
[:print:]  a visible character plus the space character
[:space:]  a whitespace character, as defined by the syntax table, but typically [ \t\r\n\v\f], which includes the newline character
[:blank:]  a space or tab character
[:xdigit:] an hexadecimal digit
[:cntrl:]  a control character
[:ascii:]  an ascii character

*?, +?, and ?? are non-greedy versions of *, +, and ? – see NonGreedyRegexp. Also, \W, \B, and \Sc match any character that does not match \w, \b, and \sc.

Characters are organized by category. Use C-u C-x = to display the category of the character under the cursor.

\ca      ascii character
\Ca      non-ascii character (newline included)
\cl      latin character
\cg      greek character

Exemple de remplacement

Remplacer ** SOCIETE+QUOI@cogip.fr par ** SOCIETE+QUOI@cogip.fr (modifié)

C-M-% :  `\*\* \([A-Za-z0-9.@\+]+\) <RET> ** \1 (modifié)

Notes : par rapport à Atom, on backslashe les parenthèses qui créent la variable à replacer. Cette variable, dans le replace, n’est pas $1 mais \1

Incrémenter un replace

Incrémenter un index de tableau. On part de

int my_array[4];
my_array[0] = 1;
my_array[1] = 2;
my_array[2] = 3

Pour vouloir

int my_array[4];
my_array[1] = 1;
my_array[2] = 2;
my_array[3] = 3
M-x query-replace-regexp \[\([0-9]+\)\] RET [\,(1+ \#1)]

Le truc, c’est \#, qui incrémente le replace

https://stackoverflow.com/questions/8472221/emacs-regexp-to-increment-decrement-array-indices

Intervertir deux chaînes de part et d’autre d’un caractère spécial (-, | e.g.)

\(\n\(.+[^|]\)\)\(| \(.+\)\) > \4 | |2\n Dans \1, on recherche la nouvelle ligne, puis dans \2 une chaine dont on exclut le pipe. Dans \3, on cherche tout ce qu’il y a après le pipe, et dans \4, tout mot depuis \3 jusqu’à la fin de la ligne Bien remplacer \n par C-q C-j dans emacs

Sélectionner toutes les lignes commençant par **

Prévisualiser le résultat d’une expression régulière

M-x re-builder ou M-x regexp-builder (la même chose)