<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ca">
	<id>http://wiki.joanillo.org/index.php?action=history&amp;feed=atom&amp;title=Expressions_Regulars</id>
	<title>Expressions Regulars - Historial de revisió</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.joanillo.org/index.php?action=history&amp;feed=atom&amp;title=Expressions_Regulars"/>
	<link rel="alternate" type="text/html" href="http://wiki.joanillo.org/index.php?title=Expressions_Regulars&amp;action=history"/>
	<updated>2026-08-30T10:18:44Z</updated>
	<subtitle>Historial de revisió per a aquesta pàgina del wiki</subtitle>
	<generator>MediaWiki 1.34.2</generator>
	<entry>
		<id>http://wiki.joanillo.org/index.php?title=Expressions_Regulars&amp;diff=249094&amp;oldid=prev</id>
		<title>Joan: /* Altres referències */</title>
		<link rel="alternate" type="text/html" href="http://wiki.joanillo.org/index.php?title=Expressions_Regulars&amp;diff=249094&amp;oldid=prev"/>
		<updated>2012-03-13T08:49:12Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Altres referències&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Pàgina nova&lt;/b&gt;&lt;/p&gt;&lt;div&gt;*http://linuxreviews.org/beginner/tao_of_regular_expressions/&lt;br /&gt;
=Introducció=&lt;br /&gt;
Acabes de veure la utilitat de les ''expressions regulars'' en el fitxer de configuració d'un proxy com el Squid. Altres vegades també hem trobat ''expressions regulars'' i hi hem passat de puntetes. Les '''expressions regulars (regex)''' serveixen per a reconeixement i detecció de cadenes (''matching'') i s'utilitzen sovint, per exemple en scripts bash (un exemple típic pot ser formatar la data amb el format que un vol). El primer exemple que pots provar en la consola és:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
line='Today is 10/12/2010 and yesterday was 9/11/2010'&lt;br /&gt;
echo &amp;quot;$line&amp;quot; | sed -r 's#([0-9]{1,2})/([0-9]{1,2})/([0-9]{4})#\3-\2-\1#g'&lt;br /&gt;
&lt;br /&gt;
Today is 2010-12-10 and yesterday was 2010-11-9&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
En aquest cas '''sed'''' és útil per transformar la cadena de text en una altra.&lt;br /&gt;
&lt;br /&gt;
=Teoria=&lt;br /&gt;
Les '''expressions regulars (regex)''' estan formades per ''caràcters'' i ''metacaràcters''. Els caràcters normals inclouen lletres (majúscules i minúscules) i números. Els metacaràcters tenen un significat especial, com es comenta més avall. En el cas més simple, una cadena com ara ''paper'' no conté ''metacaràcters'', i coincideix amb ''paperot'', ''12paper45'', però no amb ''PAPER''.&lt;br /&gt;
&lt;br /&gt;
Per veure la potència de les expressions regulars és molt important entrendre els metacaràcters:&lt;br /&gt;
&lt;br /&gt;
*. Matches any single character. For example the regular expression r.t would match the strings rat, rut, r t, but not root. &lt;br /&gt;
*$ Matches the end of a line. For example, the regular expression weasel$ would match the end of the string &amp;quot;He's a weasel&amp;quot; but not the string &amp;quot;They are a bunch of weasels.&amp;quot; &lt;br /&gt;
*^ Matches the beginning of a line. For example, the regular expression ^When in would match the beginning of the string &amp;quot;When in the course of human events&amp;quot; but would not match &amp;quot;What and When in the&amp;quot; . &lt;br /&gt;
* *(asterisc) Matches zero or more occurences of the character immediately preceding. For example, the regular expression .* means match any number of any characters. &lt;br /&gt;
*\ This is the quoting character, use it to treat the following character as an ordinary character. For example, \$ is used to match the dollar sign character ($) rather than the end of a line. Similarly, the expression \. is used to match the period character rather than any single character. &lt;br /&gt;
*[ ], [c1-c2], [^c1-c2] Matches any one of the characters between the brackets. For example, the regular expression r[aou]t matches rat, rot, and rut, but not ret. Ranges of characters can specified by using a hyphen. For example, the regular expression [0-9] means match any digit. Multiple ranges can be specified as well. The regular expression [A-Za-z] means match any upper or lower case letter. To match any character except those in the range, the complement range, use the caret as the first character after the opening bracket. For example, the expression [^269A-Z] will match any characters except 2, 6, 9, and upper case letters. &lt;br /&gt;
*\&amp;lt; \&amp;gt; Matches the beginning (\&amp;lt;) or end (\&amp;gt;) or a word. For example, \&amp;lt;the matches on &amp;quot;the&amp;quot; in the string &amp;quot;for the wise&amp;quot; but does not match &amp;quot;the&amp;quot; in &amp;quot;otherwise&amp;quot;. NOTE: this metacharacter is not supported by all applications.&lt;br /&gt;
*\( \) Treat the expression between \( and \) as a group. Also, saves the characters matched by the expression into temporary holding areas. Up to nine pattern matches can be saved in a single regular expression. They can be referenced as \1 through \9.&lt;br /&gt;
*| Or two conditions together. For example (him|her) matches the line &amp;quot;it belongs to him&amp;quot; and matches the line &amp;quot;it belongs to her&amp;quot; but does not match the line &amp;quot;it belongs to them.&amp;quot; NOTE: this metacharacter is not supported by all applications.&lt;br /&gt;
*+ Matches one or more occurences of the character or regular expression immediately preceding. For example, the regular expression 9+ matches 9, 99, 999. NOTE: this metacharacter is not supported by all applications.&lt;br /&gt;
*? Matches 0 or 1 occurence of the character or regular expression immediately preceding.NOTE: this metacharacter is not supported by all applications.&lt;br /&gt;
*\{i\}  \{i,j\} Match a specific number of instances or instances within a range of the preceding character. For example, the expression A[0-9]\{3\} will match &amp;quot;A&amp;quot; followed by exactly 3 digits. That is, it will match A123 but not A1234. The expression [0-9]\{4,6\} any sequence of 4, 5, or 6 digits. NOTE: this metacharacter is not supported by all applications.&lt;br /&gt;
=Proves=&lt;br /&gt;
Per començar a provar expressions regulars ho farem, per exemple, amb '''grep'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GREP(1)                          User Commands                         GREP(1)&lt;br /&gt;
&lt;br /&gt;
NAME&lt;br /&gt;
       grep, egrep, fgrep, rgrep - print lines matching a pattern&lt;br /&gt;
&lt;br /&gt;
SYNOPSIS&lt;br /&gt;
       grep [OPTIONS] PATTERN [FILE...]&lt;br /&gt;
       grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]&lt;br /&gt;
&lt;br /&gt;
DESCRIPTION&lt;br /&gt;
       grep  searches the named input FILEs (or standard input if no files are&lt;br /&gt;
       named, or if a single hyphen-minus (-) is given as file name) for lines&lt;br /&gt;
       containing  a  match to the given PATTERN.  By default, grep prints the&lt;br /&gt;
       matching lines.&lt;br /&gt;
...&lt;br /&gt;
REGULAR EXPRESSIONS&lt;br /&gt;
&lt;br /&gt;
       A  regular  expression  is  a  pattern that describes a set of strings.&lt;br /&gt;
       Regular expressions are constructed analogously to  arithmetic  expres-&lt;br /&gt;
       sions, by using various operators to combine smaller expressions.&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Les ''expressions regulars'' tenen algun comportament que depenen de l'eina amb què les avalues. Per tant, s'ha d'anar a la referència de l'eina (com ara grep, vi, awk, sed, perl,...) per tal de veure la compatibilitat (veure la taula de ''Regular Expressions Syntax Support'' a baix de tot de l'enllaç que se't proposa).&lt;br /&gt;
&lt;br /&gt;
fitxer '''test.txt''':&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
he is a rat&lt;br /&gt;
he is in a rut&lt;br /&gt;
the food is Rotten&lt;br /&gt;
I like root beer&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
El ''punt'' . (amb anglès ''period'') representa qualsevol caràcter. Si volem trobar totes les línies que continguin ''r.t'' (una paraula de tres caràcters que comenci per r i acabi per t):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ cat test.txt | grep r.t&lt;br /&gt;
he is a rat&lt;br /&gt;
he is in a rut&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
o bé&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ grep r.t test.txt&lt;br /&gt;
he is a rat&lt;br /&gt;
he is in a rut&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Si volem que no sigui sensible a majúscules per a la ''r'' farem:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ grep [Rr].t test.txt&lt;br /&gt;
he is a rat&lt;br /&gt;
he is in a rut&lt;br /&gt;
the food is Rotten&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Per detectar una cadena al principi de la línia fem servir el ^ (circunflex o ''caret''):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ grep ^ĥe test.txt&lt;br /&gt;
he is a rat&lt;br /&gt;
he is in a rut&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
fixem-nos que no detecta ''the''&lt;br /&gt;
&lt;br /&gt;
L'ús del circumflex té un altre ús: quan utilitzem ^ com a primer caràcter dins dels brackets [] vol dir detectar qualsevol caràcter que no estigui en el rang. Per exemple, per detectar ''he'' però que no sigui ''the'' o ''she'' farem:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ grep [^st]he test.txt&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
no troba el ''the'' (però tampoc troba ''he is a rat'' i ''he is in a rut'' perquè espera que hi hagi un caràcter abans del ''he'').&lt;br /&gt;
&lt;br /&gt;
[A-Za-z] detecta qualsevol lletra de l'alfabet, sigui majúscula o minúscula. L'expressió regular [A-Za-z][A-Za-z]* detecta una lletra seguida de 0 (cap) o més lletres.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ grep [o]* test.txt -&amp;gt; mostra les coincidències de ''cap o alguna o''&lt;br /&gt;
 he is a rat&lt;br /&gt;
he is in a rut&lt;br /&gt;
the food is Rotten&lt;br /&gt;
I like root beer&lt;br /&gt;
$ grep [o][o]* test.txt -&amp;gt; mostra les coincidències de ''cap o alguna o, després d'una o''&lt;br /&gt;
the food is Rotten&lt;br /&gt;
I like root beer&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Per especificar el número d'ocurrències que es detecten podem utilitzar les claus. Per exemple, per detectar les instàncies de ''100'' i ''1000'', però no ''10'' i ''10000'' farem: '''10\{2,3\}'''.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ echo 10 100 1000 10000 | grep &amp;quot;10\{2,3\}&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
resultat: 10 '''100 1000 1000'''0&lt;br /&gt;
&lt;br /&gt;
Fixa't bé amb la diferència amb el següent cas:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ echo 10 100 1000 10000 | grep &amp;quot;[10]\{2,3\}&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''10 100 1000 10000&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ echo 10 100 1000 | grep &amp;quot;[[:digit:]]\{2,3\}&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
resultat: '''10 100 100'''0&lt;br /&gt;
&lt;br /&gt;
Un exemple aclaridor: (\{i\} Match a specific number of instances or instances within a range of the preceding character)&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ echo 10000 | grep &amp;quot;[10]\{1\}&amp;quot; -&amp;gt; els números sempre es repeteixen ells mateixos&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''10000'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ echo 10000 | grep &amp;quot;[10]\{2\}&amp;quot; -&amp;gt; detecta l'expressió que és un número que al seu darrere hi ha un altre número (0 o 1)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''1000'''0&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ echo 10000 | grep &amp;quot;[10]\{3\}&amp;quot; -&amp;gt; detecta el número que es repeteix amb 1 o 0 tres vegades&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''100'''00&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ echo 10000 | grep &amp;quot;[10]\{4\}&amp;quot; -&amp;gt; detecta el número que es repeteix amb 1 o 0 quatre vegades&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''1000'''0&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ echo 10000 | grep &amp;quot;[10]\{5\}&amp;quot; -&amp;gt; detecta el número que es repeteix amb 1 o 0 cinc vegades&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''10000'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ echo 10000 | grep &amp;quot;[10]\{6\}&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
100000&lt;br /&gt;
&lt;br /&gt;
Com pots comprovar, les Expressions Regulars poden ser un bon embolic... però realment són molt útils i s'utilitzen molt.&lt;br /&gt;
==Corolari (el que potser utilitzaràs més)==&lt;br /&gt;
Moltes vegades es planteja trobar dins d'una col.lecció de fitxers totes les vegades que surt una determinada paraula. Imagina't que comets habitualment l'error ortogràfic ''lampara'' (en comptes de ''làmpara''). Imaginem que tenim dos fitxers dins una carpeta (però podrien ser 1000 fitxers amb una estructura de carpetes i subcarpetes).&lt;br /&gt;
&lt;br /&gt;
Crea la carpeta ''lampara'' i fica-hi aquests dos fitxers:&lt;br /&gt;
&lt;br /&gt;
'''fitxer1.txt'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
la lampara d'Aladí s'ha fos&lt;br /&gt;
no trobo la lampara del Pere&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''fitxer2.txt'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
he anat a la botiga&lt;br /&gt;
a comprar una lampara i una taula&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ find /home/joan/lampara/ -type f -print | xargs grep -i lampara | more&lt;br /&gt;
/home/joan/lampara/fitxer1.txt:la lampara d'Aladí s'ha fos&lt;br /&gt;
/home/joan/lampara/fitxer1.txt:no trobo la lampara del Pere&lt;br /&gt;
/home/joan/lampara/fitxer2.txt:a comprar una lampara i una taula&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
És una combinació de les comandes find, xargs i grep:&lt;br /&gt;
*find: search for files in a directory hierarchy&lt;br /&gt;
*xargs: build and execute command lines from standard input&lt;br /&gt;
*grep: grep, egrep, fgrep, rgrep - print lines matching a pattern (en aquest cas el ''pattern'', l'expressió regular, és senzillament ''lampara'')&lt;br /&gt;
&lt;br /&gt;
Així et serà fàcil subsanar tots els errors.&lt;br /&gt;
&lt;br /&gt;
Hi ha alguna manera de reemplaçar totes les ocurrències? (que no sigui amb un editor gràfic tipus gedit o notepad++, sinó amb CLI). vi/vim ho pot fer:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
:%s/lampara/làmpara/g&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
on %s és la comanda de substitució del vi&lt;br /&gt;
&lt;br /&gt;
Com fer-ho automàticament per a tots els fitxers? (TBD)&lt;br /&gt;
&lt;br /&gt;
=Altres referències=&lt;br /&gt;
*Sergi Tur - Acacha: http://acacha.org/mediawiki/index.php/Expressions_regulars&lt;br /&gt;
*http://www.thegeekstuff.com/2009/03/15-practical-unix-grep-command-examples/&lt;br /&gt;
*http://en.wikipedia.org/wiki/Regular_expression&lt;br /&gt;
*what-is-the-most-difficult-challenging-regular-expression: http://stackoverflow.com/questions/800813/what-is-the-most-difficult-challenging-regular-expression-you-have-ever-written&lt;/div&gt;</summary>
		<author><name>Joan</name></author>
		
	</entry>
</feed>