

{"id":110803,"date":"2022-09-04T11:00:41","date_gmt":"2022-09-04T05:30:41","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=110803"},"modified":"2022-10-04T12:40:11","modified_gmt":"2022-10-04T07:10:11","slug":"regular-expression-in-linux","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/regular-expression-in-linux\/","title":{"rendered":"Regular Expression in Linux"},"content":{"rendered":"<p>In this article, you will learn all there is to shell programming in Linux-based operating systems. We will go through what regex is, why we use them, what are the different regular expression in linux, the types of regex, and examples of each in detail. So sit down, grab a snack, and read right till the end!<\/p>\n<h3>What is Linux Regex?<\/h3>\n<p>First of all, regex is the abbreviation for \u201cRegular Expressions\u201d. However, they are not as regular as they sound! Regular expressions are special characters in Linux-based operating systems that help us search data and match a complex pattern.<\/p>\n<p>Regular expressions in linux are most commonly used with commands like grep, sed, tr, ed, awk, and vi. However, we will be focusing on the usage of regular expressions with the grep command in this article, not to mention that there will be a few honorable mentions of other commands as well!<\/p>\n<p>Regex is a really powerful command-line-based tool that helps in describing several sequences of characters. REGEX is also called as REGEXP.<\/p>\n<h3>Types of Regular Expressions in Linux<\/h3>\n<p>In this article let us take divide the regular expressions into the following 3 types while trying the understand REGEX. We will also look at how to use each expression in the terminal to. The 3 types of REGEX are:<\/p>\n<p>1. Basic regular expressions<\/p>\n<p>2. Interval regular expressions<\/p>\n<p>3. Entended regular expressions<\/p>\n<p>Let us now take a closer look at each of these types:<\/p>\n<h4>1. Basic regular expressions<\/h4>\n<p>Before we look at the practical examples of the basic regular expressions, let us cumulatively look at the list of the basic regular expressions:<\/p>\n<h5>a. .<\/h5>\n<p>This basic regular expression replaces any character.<\/p>\n<h5>b. ^<\/h5>\n<p>This basic regular expression matches the start of the string.<\/p>\n<h5>c. $<\/h5>\n<p>This basic regular expression matches the end of the string.<\/p>\n<h5>d. *<\/h5>\n<p>This basic regular expression matches up zero or more times the preceding character.<\/p>\n<h5>e. \\<\/h5>\n<p>This basic regular expression represents special characters.<\/p>\n<h5>f. ()<\/h5>\n<p>This basic regular expression groups regular expressions.<\/p>\n<h5>g. ?<\/h5>\n<p>This basic regular expression matches up with exactly one character<\/p>\n<p>Let us now look at an example for each of the basic regexes:<\/p>\n<p>We know that to use regular expressions we need a text file, for the sake of an example, let us consider the file \u201cfruits.txt\u201d that contains a very long list of fruits!<\/p>\n<h5>a. Using \u2018dot\u2019 to match string<\/h5>\n<p>Using the dot (.) expression, we can try to find a string even if we don\u2019t know the full string. We can use the dot expression in places of the character we don\u2019t know.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/using-dot-to-match-string.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110861\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/using-dot-to-match-string.webp\" alt=\"using dot to match string\" width=\"590\" height=\"99\" \/><\/a><\/p>\n<p>In the above example, even though we specified the dot expression in the place of \u201cl\u201d in the text \u201cApple\u201d, the command gave us the lines that contain the text \u201cApple\u201d.<\/p>\n<h5>b. Using \u2018caret\u2019 to match the beginning of the string<\/h5>\n<p>We can use the caret (^) expression to search for lines that begin with the specified text.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/using-caret-to-match-the-beginning-of-the-string.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110863\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/using-caret-to-match-the-beginning-of-the-string.webp\" alt=\"using caret to match the beginning of the string\" width=\"558\" height=\"254\" \/><\/a><\/p>\n<p>The above command gave us all the strings beginning with the letter \u201cB\u201d.<\/p>\n<h5>c. Using \u2018dollar\u2019 to match the end of the string<\/h5>\n<p>Just like we have the expression \u201c^\u201d to match the beginning of a string we also have the expression \u201c$\u201d to search to match the end of the string.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/using-dollar-to-match-the-end-of-the-string.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110864\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/using-dollar-to-match-the-end-of-the-string.webp\" alt=\"using dollar to match the end of the string\" width=\"562\" height=\"262\" \/><\/a><\/p>\n<p>The command in the example above prints all the strings that end with the letter \u201ce\u201d.<\/p>\n<h5>d. Using \u2018asterisk\u2019 to find the repetition of a letter<\/h5>\n<p>Use the asterisk (*) expression to match a repetition of a letter in a word. You can print the repetitions all the way from zero to infinite!<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/using-asterisk-to-find-the-repetition-of-a-letter.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110865\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/using-asterisk-to-find-the-repetition-of-a-letter.webp\" alt=\"using asterisk to find the repetition of a letter\" width=\"594\" height=\"166\" \/><\/a><\/p>\n<p>The command in the example prints the words that match the contain the text \u201cAple\u201d and have any repetitions of the letter \u201cp\u201d, meaning that it will even print out strings like \u201cAppple\u201d, \u201cAppppppple\u201d, Appppppppppple\u201d and so on if they exist.<\/p>\n<h5>e. Using \u2018backslash\u2019 to match a special symbol<\/h5>\n<p>If we want to search for special characters like semicolon (;), colon (:), slashes(\/), comma (,) and many more, we use the expression \u2018backslash\u2019. We specify the special character you want to search for after the backslash expression.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/using-backslash-to-match-a-special-symbol.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110866\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/using-backslash-to-match-a-special-symbol.webp\" alt=\"using backslash to match a special symbol\" width=\"678\" height=\"336\" \/><\/a><\/p>\n<p>The command shown in the above screenshot displays all the strings that have a space in them.<\/p>\n<h5>f. Using \u2018braces\u2019 to match a group of regexp<\/h5>\n<p>If we simply want to search for a piece of text in a file, we use the bracket expressions and specify the word we want to search for in them. It must be noted that while using the braces expression with the grep command, we must make use of the option \u201c-E\u201d which is an extended regular expression.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/using-braces-to-match-a-group-of-regexp.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110867\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/using-braces-to-match-a-group-of-regexp.webp\" alt=\"using braces to match a group of regexp\" width=\"666\" height=\"301\" \/><\/a><\/p>\n<p>In the above screenshot, the command prints out all the lines with the text \u201cfruit\u201d in them.<\/p>\n<h5>g. Using \u2018?\u2019 to print all the matching characters<\/h5>\n<p>If you want to print out the lines that contain either one of the characters you specify or all of the characters you specify.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/using-question-mark-to-print-all-the-matching-characters.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110868\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/using-question-mark-to-print-all-the-matching-characters.webp\" alt=\"using question mark to print all the matching characters\" width=\"596\" height=\"394\" \/><\/a><\/p>\n<p>The command shown in the above screenshot prints all the lines that either start with \u201cc\u201d, or \u201cch\u201d. However, if we run the exact same command but without the \u201c?\u201d expression, we will get the line that starts with \u201cCh\u201d as shown:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/testing-output-without-using-the-question-mark-regex.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110869\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/testing-output-without-using-the-question-mark-regex.webp\" alt=\"testing output without using the question mark regex\" width=\"595\" height=\"142\" \/><\/a><\/p>\n<h4>2. Interval regular expressions<\/h4>\n<p>These expressions print out the lines that match the occurrence of the character or characters we specify. These are more sophisticated yet simple, let us look at them:<\/p>\n<h5>a. {n}<\/h5>\n<p>This interval regular expression matches the preceding characters that appear exactly \u201cn\u201d number of times.<\/p>\n<h5>b. {n,m}<\/h5>\n<p>This interval regular expression matches the preceding character that appears exactly \u201cn\u201d number of times but not more than \u201cm\u201d, meaning it prints repetitions of the character between \u201cn\u201d to \u201cm\u201d number of times.<\/p>\n<h5>c. {n,}<\/h5>\n<p>This interval regular expression matches the preceding character that appears \u201cn\u201d number of times or more.<\/p>\n<p>Let us now look at an example for each of the 3 interval regular expressions along with the grep command:<\/p>\n<h5>a. Using the \u201c{n}\u201d expression<\/h5>\n<p>In the command shown below, we used the expression {n} to search for words that have 2 occurrences of the character \u201cp\u201d.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/using-the-n-expression.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110870\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/using-the-n-expression.webp\" alt=\"using the {n} expression\" width=\"620\" height=\"212\" \/><\/a><\/p>\n<h5>b. Using the \u201c{n,m}\u201d expression<\/h5>\n<p>In the command shown below, we used the expression {n,m} to search for words that have at least 1 occurrence of \u201cp\u201d and at most 2 occurrences of \u201cp\u201d<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/using-the-nm-expression.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110871\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/using-the-nm-expression.webp\" alt=\"using the {n,m} expression\" width=\"635\" height=\"616\" \/><\/a><\/p>\n<h5>c. Using the \u201c{n,}\u201d expression<\/h5>\n<p>In the command shown below, we used the expression {n,} to search for words that have the character \u201cp\u201d at least twice.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/using-the-n-expression-1.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110872\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/using-the-n-expression-1.webp\" alt=\"using the {n,} expression\" width=\"628\" height=\"210\" \/><\/a><\/p>\n<h4>3. Extended regular expressions<\/h4>\n<p>These expressions help us in finding text where a pattern of string either precedes or succeeds another piece of string. The following are the extended regular expressions:<\/p>\n<h5>a. \\+<\/h5>\n<p>This extended regular expression matches one or more occurrences of the previous character.<\/p>\n<h5>b. \\?<\/h5>\n<p>This extended regular expression matches zero or more occurrences of the previous character.<\/p>\n<p>Let us look at an example for each of the 2 extended regular expressions:<\/p>\n<h5>a. Using \u201c\\+\u201d<\/h5>\n<p>The command in the screenshot below prints all of the occurrences of the cases where the character \u201ct\u201d is preceded by the character \u201ca\u201d.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/using-followed-by-a-slash.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110873\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/using-followed-by-a-slash.webp\" alt=\"using followed by a slash\" width=\"595\" height=\"212\" \/><\/a><\/p>\n<h5>b. Using \u201c\\?\u201d<\/h5>\n<p>The command in the screenshot below prints all of the occurrences of the cases where the character \u201ct\u201d is preceded by the character \u201ca\u201d and also where only the character \u201ct\u201d is present.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/using-slah-followed-by-.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110874\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/using-slah-followed-by-.webp\" alt=\"using slash followed by\" width=\"596\" height=\"498\" \/><\/a><\/p>\n<h3>Brace expansion in Linux<\/h3>\n<p>Here is a bonus example of a regular expression &#8211; {}. Using brace expansion we can specify a range of things to perform operations on, here are some examples:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/examples-of-brace-expansions.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110875\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/examples-of-brace-expansions.webp\" alt=\"examples of brace expansions\" width=\"598\" height=\"257\" \/><\/a><\/p>\n<p>One such real-life example of brace expansion is when downloading a continuous range of websites using the wget command:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/using-brace-expansion-with-the-wget-command.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110876\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/using-brace-expansion-with-the-wget-command.webp\" alt=\"using brace expansion with the wget command\" width=\"780\" height=\"552\" \/><\/a><\/p>\n<p>We can use expressions along with many other multiple commands also.<\/p>\n<h3>Table of metacharacters<\/h3>\n<p>Even though we have used most of the metacharacter, let us look at everything in one table to get s better picture:<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>NO<\/strong><\/td>\n<td><strong>EXPRESSION<\/strong><\/td>\n<td><strong>DESCRIPTION<\/strong><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">1<\/span><\/td>\n<td><span style=\"font-weight: 400;\">.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This metacharacter replaces any character.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">2<\/span><\/td>\n<td><span style=\"font-weight: 400;\">^<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This metacharacter matches the start of the string and represents characters not in the string.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">3<\/span><\/td>\n<td><span style=\"font-weight: 400;\">$<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This metacharacter matches the end of the string.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">4<\/span><\/td>\n<td><span style=\"font-weight: 400;\">*<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This metacharacter Matches zero or more times the preceding character.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">5<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\\<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This metacharacter represents the group of characters.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">6<\/span><\/td>\n<td><span style=\"font-weight: 400;\">()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This metacharacter Group regular expressions.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">7<\/span><\/td>\n<td><span style=\"font-weight: 400;\">?<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This metacharacter Matches exactly one character.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">8<\/span><\/td>\n<td><span style=\"font-weight: 400;\">+<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This metacharacter matches one or more times the preceding character.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">9<\/span><\/td>\n<td><span style=\"font-weight: 400;\">{N}<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Preceding character is matched exactly N times.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">10<\/span><\/td>\n<td><span style=\"font-weight: 400;\">{N,}<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Preceding character is matched exactly N times or more.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">11<\/span><\/td>\n<td><span style=\"font-weight: 400;\">{N, M}<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Preceding character is matched exactly N times, but not more than N times.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">12<\/span><\/td>\n<td><span style=\"font-weight: 400;\">&#8211;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This metacharacter represents the range.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">13<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\\b<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This metacharacter matches the empty string at the edge of a word.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">15<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\\B<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This metacharacter matches the empty string if it is not at the edge of a word.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">16<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\\&lt;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This metacharacter matches the empty string at the beginning of a word.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">17<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\\&gt;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This metacharacter matches the empty string at the end of a word.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Shell scripting using Regular Expression in Linux<\/h3>\n<p>We can also use regular expressions in shell scripting, here are some examples:<\/p>\n<h4>1. Using \u201c^\u201d in shell scripting<\/h4>\n<p>Here is a shell program to print words starting the with letter \u201cB\u201d.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/using-caret-in-shell-scripting.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110877\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/using-caret-in-shell-scripting.webp\" alt=\"using caret in shell scripting\" width=\"785\" height=\"140\" \/><\/a><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/output-of-shell-program-using-caret.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110878\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/output-of-shell-program-using-caret.webp\" alt=\"output of shell program using caret\" width=\"698\" height=\"263\" \/><\/a><\/p>\n<h4>2. Using \u201c*\u201d in shell scripting<\/h4>\n<p>Here is a shell program to print words having occurrences of \u201cap\u201d in them.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/using-asterisk-in-shell-program.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110879\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/using-asterisk-in-shell-program.webp\" alt=\"using asterisk in shell program\" width=\"780\" height=\"94\" \/><\/a><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/output-of-the-shell-program-using-asterisk.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110880\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/output-of-the-shell-program-using-asterisk.webp\" alt=\"output of the shell program using asterisk\" width=\"780\" height=\"133\" \/><\/a><\/p>\n<h4>3. Using \u201c?\u201d in shell scripting<\/h4>\n<p>Here is a shell program to print words having occurrences of \u201cch\u201d in them<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/using-question-mark-in-shell-program.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110881\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/using-question-mark-in-shell-program.webp\" alt=\"using question mark in shell program\" width=\"780\" height=\"160\" \/><\/a><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/output-of-shell-program-using-question-mark.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-110882\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/08\/output-of-shell-program-using-question-mark.webp\" alt=\"output of shell program using question mark\" width=\"702\" height=\"436\" \/><\/a><\/p>\n<h3>Summary<\/h3>\n<p>As you have seen, regular expressions are a simple set of operators that make life so much easier as they improve th efficiency of workflow. You have now learned what operators are, why they are used and the types of operators, where we covered the basic, interval, and extended types of regular expressions along with examples.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, you will learn all there is to shell programming in Linux-based operating systems. We will go through what regex is, why we use them, what are the different regular expression in&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":110805,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[27095,27094],"class_list":["post-110803","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","tag-linux-regex","tag-regular-expression-in-linux"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Regular Expression in Linux - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn about regular expression in Linux. See why we use them, what are different regular expressions, types of regex with examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/regular-expression-in-linux\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Regular Expression in Linux - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn about regular expression in Linux. See why we use them, what are different regular expressions, types of regex with examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/regular-expression-in-linux\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-04T05:30:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-10-04T07:10:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/regex-in-linux.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Regular Expression in Linux - DataFlair","description":"Learn about regular expression in Linux. See why we use them, what are different regular expressions, types of regex with examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/regular-expression-in-linux\/","og_locale":"en_US","og_type":"article","og_title":"Regular Expression in Linux - DataFlair","og_description":"Learn about regular expression in Linux. See why we use them, what are different regular expressions, types of regex with examples.","og_url":"https:\/\/data-flair.training\/blogs\/regular-expression-in-linux\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2022-09-04T05:30:41+00:00","article_modified_time":"2022-10-04T07:10:11+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/regex-in-linux.webp","type":"image\/webp"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/regular-expression-in-linux\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/regular-expression-in-linux\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/b49855299264df5e27e3ec6c2cd9fde9"},"headline":"Regular Expression in Linux","datePublished":"2022-09-04T05:30:41+00:00","dateModified":"2022-10-04T07:10:11+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/regular-expression-in-linux\/"},"wordCount":1565,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/regular-expression-in-linux\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/regex-in-linux.webp","keywords":["linux regex","Regular Expression in Linux"],"articleSection":["Linux Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/regular-expression-in-linux\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/regular-expression-in-linux\/","url":"https:\/\/data-flair.training\/blogs\/regular-expression-in-linux\/","name":"Regular Expression in Linux - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/regular-expression-in-linux\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/regular-expression-in-linux\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/regex-in-linux.webp","datePublished":"2022-09-04T05:30:41+00:00","dateModified":"2022-10-04T07:10:11+00:00","description":"Learn about regular expression in Linux. See why we use them, what are different regular expressions, types of regex with examples.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/regular-expression-in-linux\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/regular-expression-in-linux\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/regular-expression-in-linux\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/regex-in-linux.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2022\/06\/regex-in-linux.webp","width":1200,"height":628,"caption":"regular expression in linux"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/regular-expression-in-linux\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Linux Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/linux\/"},{"@type":"ListItem","position":3,"name":"Regular Expression in Linux"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/b49855299264df5e27e3ec6c2cd9fde9","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team is a group of passionate educators and industry experts dedicated to providing high-quality online learning resources on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. With years of experience in the field, the team aims to simplify complex topics and help learners advance their careers. At DataFlair, we believe in empowering students and professionals with the knowledge and skills needed to thrive in today\u2019s fast-paced tech industry. Follow us for Free courses, expert insights, tutorials, and practical tips to boost your learning journey.","url":"https:\/\/data-flair.training\/blogs\/author\/datafbdad\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/110803","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=110803"}],"version-history":[{"count":3,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/110803\/revisions"}],"predecessor-version":[{"id":110883,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/110803\/revisions\/110883"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/110805"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=110803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=110803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=110803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}