Dominio de las expresiones regulares (Regex)
Comprenda la sintaxis y la potencia de las expresiones regulares para realizar búsquedas avanzadas de patrones.
Dominio de las expresiones regulares (Regex) es una lección gratuita de Linux Command Line Mastery en CoddyKit. Esta es la lección 1 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Linux Command Line Mastery, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Linux Command Line Mastery incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
Intro to Regular Expressions
Welcome to Regular Expressions (Regex)! Regex is a powerful mini-language used for pattern matching in text. It's like a super-charged search tool.
You can use Regex to search, replace, and validate specific text patterns in files or command output. Mastering it will make you much more efficient at the command line!
Matching Exact Text
The simplest Regex matches literal characters. This means the pattern will look for the exact sequence of characters you provide.
For example, searching for apple will only find occurrences of "apple".
echo "I have an apple and a pineapple." | grep 'apple'Any Single Character: The Dot (.)
The dot (.) is a special character, called a "metacharacter". It matches any single character, except for a newline. Use it when you don't care what character is in a specific spot.
echo "cat, cot, cut, c@t" | grep 'c.t'Matching Repetition: Quantifiers
Quantifiers let you specify how many times a character or group should appear. These are powerful for flexible matching:
*: Matches zero or more occurrences.+: Matches one or more occurrences.?: Matches zero or one occurrence (makes it optional).
echo "color colour coor" | grep 'colou*r'Specific Choices: Character Classes
Use square brackets ([]) to match any one character from a specified set. This defines specific options for a single position in your pattern.
For example, [aeiou] would match any single vowel.
echo "grey gray" | grep 'gr[ae]y'Defining Ranges in Classes
Inside character classes, you can specify ranges of characters using a hyphen (-). This makes your patterns much shorter and easier to read.
[0-9]: Matches any digit.[a-z]: Matches any lowercase letter.[A-Z]: Matches any uppercase letter.[a-zA-Z]: Matches any letter (case-insensitive).
echo "num1 num2 numA" | grep 'num[0-9]'Excluding Characters: Negation
To match any character that is NOT in a specified set, place a caret (^) as the first character inside the square brackets ([^...]).
echo "apple, banana, 123, !@#" | grep '[^a-z]'Start and End: Anchors
Anchors don't match characters; they match positions. They "anchor" your pattern to the start or end of a line.
^: Matches the beginning of a line.$: Matches the end of a line.
Note: When ^ is used outside of [], it's an anchor, not a negation!
echo -e "start here\n here end\nstart and end" | grep '^start'Putting It All Together
Let's combine some of these concepts. Imagine you want to find lines that start with a capital letter, followed by any number of lowercase letters, and end with a digit.
echo -e "Hello1\nworld2\nAnotherTest3" | grep '^[A-Z][a-z]*[0-9]$'Regex Challenge
Consider the following text:
apple
apples
apply
app
AppleWhich regular expression would match "apple" and "apples", but NOT "apply", "app", or "Apple"?
Recap: Regex Fundamentals
In this lesson, you've taken your first steps into the powerful world of Regular Expressions! We covered:
- Literal matching for exact text.
- The
.metacharacter for any single character. - Quantifiers (
*,+,?) for repetition. - Character classes (
[]) and ranges ([a-z]) for specific sets. - Negation (
[^]) to exclude characters. - Anchors (
^,$) for start and end of lines.
These are the building blocks for creating sophisticated text patterns. Keep practicing!
Preguntas frecuentes
¿La lección «Dominio de las expresiones regulares (Regex)» es gratis?
Sí — el texto completo de «Dominio de las expresiones regulares (Regex)» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Linux Command Line Mastery, actualiza a CoddyKit PRO. El curso de Linux Command Line Mastery incluye 4 lecciones en total.
¿Qué aprenderé en «Dominio de las expresiones regulares (Regex)»?
Comprenda la sintaxis y la potencia de las expresiones regulares para realizar búsquedas avanzadas de patrones. Practicas Linux Command Line Mastery con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Linux Command Line Mastery?
No se requiere experiencia previa. Linux Command Line Mastery en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 1 de 4.
¿Cuánto tiempo toma la lección «Dominio de las expresiones regulares (Regex)»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Linux Command Line Mastery?
Sí. Cada lección de Linux Command Line Mastery incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Dominio de las expresiones regulares (Regex)
- Patrones avanzados de `grep` y `find`
- `xargs` para encadenar comandos
- Edición de flujos a gran escala con sed y tr