{"id":28741,"date":"2021-01-26T13:55:23","date_gmt":"2021-01-26T21:55:23","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=28741"},"modified":"2022-07-20T08:35:05","modified_gmt":"2022-07-20T15:35:05","slug":"python-regex","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/python-regex\/","title":{"rendered":"Python Regex: An Introduction to Using Regular Expressions"},"content":{"rendered":"\n<p>As a new developer, regular expressions (or regex as it\u2019s commonly known), can be daunting because of the strange, unfamiliar syntax: &nbsp;&nbsp;&nbsp; <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> ^<strong>[<\/strong>A-Za-z0-9._%+-<strong>]<\/strong>+@<strong>[<\/strong>A-Za-z0-9.-<strong>]<\/strong>+\\.<strong>[<\/strong>A-Za-z<strong>]<\/strong>{2,}$ <\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>When I was a new developer and saw the expression above for the first time, I remember my first thought was \u201c<em>say what? I\u2019m never going to be able to learn that gobbledygook. That\u2019s too much for me. I can\u2019t do it.<\/em>\u201d<br><\/p>\n\n\n\n<p>But you can! It\u2019s just a matter of getting over that initial overwhelming feeling that regular expressions are too foreign. Just like learning or speaking a foreign language \u2014 once you get the hang of it, it will come pretty naturally. I promise!<br><\/p>\n\n\n\n<p>In this article, we take a look at what regular expressions are, why they are used, and how to use them in Python. By the time you finish this article, you\u2019ll have a solid understanding of regular expressions so you can interpret what the expression above means!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are Regular Expressions?&nbsp;<\/h2>\n\n\n\n<p>When we take a look at what a regular expression is, we need to remind ourselves of what a string is. Remember that a string is just a collection of characters that are <em>strung together<\/em> and bounded by a set of quotation marks:<br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> \u201cHello World\u201d\n\n\u201c555-555-5555\u201d\n\n&nbsp;&nbsp;&nbsp;\u201cJohn Doe\n123 Main St.\nAnywhere, USA 99999-9999\u201d\n\n&nbsp;&nbsp;\u201cemail@email.io\u201d<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>These are all examples of strings. They can be of any length, or no length at all. Regular Expressions, or more commonly shortened to <em>regex<\/em>, is an expression whose components match patterns up with strings to find out some sort of information.<br><\/p>\n\n\n\n<p>What\u2019s great about regex is that it doesn\u2019t necessarily care about the language you are coding in \u2014 it\u2019s fairly language agnostic. The difference comes in the language\u2019s methods and how to perform actions using those expressions.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What are Regular Expressions Used For?<\/h3>\n\n\n\n<p>Regex is a pattern that we would want to specifically look for in a string. We can use regex to search for a particular phrase or pattern to replace it with something else, or we can validate forms to be certain a user is entering information in a certain format so that it is consistent across all users.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Search and Replace<\/h4>\n\n\n\n<p>Say for instance we have a phone number presented in this format:<br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> 555 555 5555 <\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>This is a valid format for a phone number in the United States. But what if we wanted to replace the spaces with dashes? Or add parentheses around the area code and a dash to make it more readable?<\/p>\n\n\n\n<p>We can use regular expressions for that! We\u2019ll go over in the next section how to do that with Python \u2014 for now I want you to get the general feel for what you can do with regular expressions and how they can be useful.<br><\/p>\n\n\n\n<p>The result we would see after a search and replace on a phone number in Python for the format we would like to use would be:<br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">555-555-5555\n     or\n(555)555-5555<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>There is no need to hardcode or look for a specific value or index of the string in regular expressions because we can just look for the patterns in the strings and manipulate all of the records we have to match.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Validate<\/h4>\n\n\n\n<p>Have you ever filled something in on a site only for it to display an error message because of a missed symbol or a pattern you didn\u2019t follow? More than likely regular expressions were used to make sure your input matched what their database is looking for.<br><\/p>\n\n\n\n<p>This is called validation and is super useful when building forms to be certain that a phone number follows the format you\u2019d like for it to be in or that an email address is a properly formatted email address, or a password matches the parameters you have set for it to be a valid password (length, special characters, digits, upper or small case, etc).&nbsp;<br><\/p>\n\n\n\n<p>This helps prevent errors in your database by alerting the user to typos or mismatched patterns.&nbsp;<br><\/p>\n\n\n\n<p>In the next section, we will take a look at the components or <em>pattern matchers<\/em> that build regular expressions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Types of Regex Pattern Matchers<\/h2>\n\n\n\n<p>Literal characters, metacharacters, and quantifiers make up the types of pattern matchers we see in regex. A pattern matcher is a character that is used to help find a pattern in a string. It is the primary building block of a regular expression.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Literal Characters<\/h3>\n\n\n\n<p>The most basic example of a pattern matcher in regex is a literal character. It matches a hard coded character or string.<br><\/p>\n\n\n\n<p>Examples:&nbsp;<\/p>\n\n\n\n<p><code>hello<\/code> \u21d2 collection of five distinct characters.<\/p>\n\n\n\n<p>When a regex pattern is applied here, it looks for each of these characters in succession. \u201chello\u201d, \u201chelloing\u201d, or \u201chelloed\u201d would pass a pattern check, but \u201cHello\u201d, \u201chelo\u201d, or \u201cHeLlo\u201d would not.<br><\/p>\n\n\n\n<p><code>A<\/code> \u21d2 collection of one distinct character.<\/p>\n\n\n\n<p>Because regex looks for distinct characters, it is case sensitive too. So \u201cA\u201d would pass, but \u201ca\u201d would not. We\u2019ll get into this more in a little bit.&nbsp;<br><\/p>\n\n\n\n<p><code>A simple sentence\\<\/code>. \u21d2 collection of several distinct characters.<br><\/p>\n\n\n\n<p>Regex looks for every character in the expression in succession when it looks at a string. If \u201cA simple sentence\u201d is not in the searched string exactly as it is written in the regex, it would not pass.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Escape Characters<\/h4>\n\n\n\n<p>Take a look at the last example. Notice that there is a \\ in front of the period. A dot in regular expression syntax is synonymous with keywords in languages like JavaScript or Python.<br><\/p>\n\n\n\n<p>You can&#8217;t use a period\/dot on its own if you want it to be part of a pattern in regular expressions. You have to escape the character in order for the regular expression engine to interpret that as a literal representation of a period instead of the regex meaning.&nbsp;<br><\/p>\n\n\n\n<p>Here are some other special sequences of characters that need to be escaped if you want the literal character instead of the translated meaning that the regex engine compiles it to.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Asterisk *<\/li><li>Backslash \/<\/li><li>Plus +<\/li><li>Caret ^<\/li><li>Dollar Sign $<\/li><li>Dot\/Period .<\/li><li>Pipe |<\/li><li>Question Mark ?<\/li><li>Parentheses \u2013 both types ()<\/li><li>Curly Braces \u2013 both types {}<\/li><\/ul>\n\n\n\n<p>Literal characters in regex match exactly with the character you include as part of the pattern. If you want to include a character that\u2019s listed above, be sure to escape it so that it can also be a part of your regex.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Common Matchers<\/h3>\n\n\n\n<p>The purpose of a matcher is to match multiple letters in a pattern. This collection of pattern matching symbols is fairly consistent among the programming languages that use regex.<br><\/p>\n\n\n\n<table class=\"wp-block-table course-info-table\"><tbody><tr><td>Matcher<\/td><td>Description<\/td><td>Example<\/td><\/tr><tr><td>.<\/td><td>Matches any character<\/td><td>n.w would match <em>now, naw, or new<\/em>, etc. Any character passes the test<\/td><\/tr><tr><td>^regex<\/td><td>Looks for pattern at beginning of the line<\/td><td>^hello would match hello in a line that started with that pattern<\/td><\/tr><tr><td>regex$<\/td><td>Looks for pattern at end of the line<\/td><td>world$ would match world in a line that ended with that pattern<\/td><\/tr><tr><td>[abc]<\/td><td>Matches a, b, or c&nbsp;<\/td><td>[misp]is considered a <em>set<\/em> and would match any string that has any of those characters in it.&nbsp;<br>For example, it could match all the individual letters in mississippi, and miss, but only some of the letters in marsh, and missouri<\/td><\/tr><tr><td>[abc][xyz]<\/td><td>Matches a, b, or c followed by x, y, or z<\/td><td>\/[Mm][sip]\/ would match any string that starts with M or m, followed by a set that has any of the characters in [sip]<\/td><\/tr><tr><td>[^abc]<\/td><td>Not a, b, or c<\/td><td>[^rstlne] would match any character that is not r, s, t, l, n, or e<\/td><\/tr><tr><td>[a-zA-Z0-9]<\/td><td>Matches any character within the range<\/td><td>[a-n] would match any character between a and n. <em>and, end, blind, can<\/em>, all have characters that entirely match here<\/td><\/tr><tr><td>A|B<\/td><td>A or B<\/td><td>M|m. would match any word or phrase at least two characters in length that matches either an M or an m plus at least one or more other characters.<\/td><\/tr><tr><td>CAT<\/td><td>Matches C, followed by A, followed by T<\/td><td>hello world would match <em>hello world<\/em> exactly<br><\/td><\/tr><\/tbody><\/table>\n\n\n\n<h3 class=\"wp-block-heading\"><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">Metacharacters<\/h3>\n\n\n\n<p>Regular expressions also use metacharacters to describe a pattern. Metacharacters have some sort of meaning behind them and will describe the shape of the pattern.<br><\/p>\n\n\n\n<table class=\"wp-block-table course-info-table\"><tbody><tr><td>Metacharacter<\/td><td>Description<\/td><td>Example<\/td><\/tr><tr><td>\\d<\/td><td>Matches any digit<\/td><td>\\d would match 1, 2, or 3, etc. Shorthand for [0-9]<\/td><\/tr><tr><td>\\D<\/td><td>Matches any non-digit character<\/td><td>\\D would match A, B, g, etc.. Shorthand for [^0-9]&nbsp;<\/td><\/tr><tr><td>\\s<\/td><td>Matches any whitespace character<\/td><td>\\s would match new lines, tabs, spaces, etc.&nbsp;<br><\/td><\/tr><tr><td>\\S<\/td><td>Matches any non-whitespace character&nbsp;<\/td><td>\\S would match any character except a whitespace character.&nbsp;<\/td><\/tr><tr><td>\\w<\/td><td>Matches any word character<\/td><td>A word character, short for [a-zA-Z_0-9]<\/td><\/tr><tr><td>\\W<\/td><td>Matches any non-word character<\/td><td>[\\W] would match any special characters. Shorthand for [^\\w]<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p><strong>Note<\/strong>: Capital letter metacharacters (\\W, \\D, etc) usually correspond to the opposite of what the lowercase letter metacharacters do (\\w, \\d, etc).&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Quantifiers<\/h3>\n\n\n\n<table class=\"wp-block-table course-info-table\"><tbody><tr><td>Quantifier<\/td><td>Description<\/td><td>Example<\/td><\/tr><tr><td>+<\/td><td>One of more of preceding character<\/td><td>\\d+ would match two or more digits<\/td><\/tr><tr><td>*<\/td><td>Zero or more of preceding character<\/td><td>.* would match any character 0 or more times&nbsp;<br>Note: Technically an empty string would fulfill this regex!<\/td><\/tr><tr><td>?<\/td><td>Zero or one of the preceding character<\/td><td>a?.* would match <em>a, any, hello, world&nbsp;<\/em><\/td><\/tr><tr><td>{number}<\/td><td>Matches preceding character exactly <em>number<\/em> of times&nbsp;<\/td><td>\\d{3} matches exactly three digits [0-9]<\/td><\/tr><tr><td>{num1,num2}<\/td><td>Matches preceding character in a range of nums<\/td><td>\\d{3,5} matches 3 to 5 digits that are [0-9]<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Use the quantifiers, metacharacters, and other matchers as the building blocks for your regular expressions. The syntax mentioned here is similar across multiple languages that use regular expressions.<br><\/p>\n\n\n\n<p>However, there are some things that are used in Ruby or JavaScript, for instance, that would not be transferable to Python.<br><\/p>\n\n\n\n<p>Let\u2019s learn a little more about how regular expressions work in Python in the next section.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Do Regular Expressions Work in Python?<\/h2>\n\n\n\n<p>To use regular expressions in Python import the <code>re<\/code> module into the top of your file.&nbsp;<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>import re \nstring = &quot;The quick brown fox jumped over the lazy dog&quot;\n \n \nresult = re.search(&quot;q.+k\\s&quot;, string) # this is the match object if it returns a positive result. It will return a NoneType object otherwise\nprint(result.span(), &quot;&lt;== This is the tuple containing the span of indexes the result is in&quot;)\nprint(result.string, &quot;&lt;== This is the original string &quot;)\nprint(result.group(), &quot;&lt;== This is the group of characters that match our regex pattern&quot;)\n\n===================================================================\n\nimport re \nstring = &quot;&quot;&quot;The \nquick brown \nfox jumped over the lazy dog\n&quot;&quot;&quot;\n\nresult = re.search(&quot;&quot;&quot;\n\t\t\t\t\t^ # beginning of line\n                    Q # literal character 'Q'\n                    . # dot ==&gt; any character\n                    + # quantifier == more than 1\n                    k # literal character 'k'\n                    \\s # special character '\\s'\n                    &quot;&quot;&quot;, string, flags=re.IGNORECASE | re.M | re.VERBOSE)\n\nif result:                                 \n  print(result.span(), &quot;&lt;== This is the tuple containing the span of indexes the result is in&quot;)\n  print(result.string, &quot;&lt;== This is the original string &quot;)\n  print(result.group(), &quot;&lt;== This is the group of characters that match our regex pattern&quot;)\nelse:\n\tprint(result)\n\n\n===================================================================\n\nimport re \nprint(re.match(&quot;quick&quot;, str, flags=re.IGNORECASE))\nprint(re.search(&quot;quick&quot;, str, flags=re.IGNORECASE | re.MULTILINE))\n\n===================================================================\n\nimport re \nphone = &quot;555 555 5555&quot;\ncorrectFormat = re.sub(&quot;\\s&quot;, &quot;-&quot;, phone)\nprint(correctFormat)\n\n===================================================================\n\nimport re \n\nleft_parens = re.sub(&quot;^&quot;, &quot;(&quot;, phone) \nright_parens = re.sub(&quot;\\s&quot;, &quot;)&quot;, left_parens, 1)\nsecondCorrectFormat = re.sub(&quot;\\s&quot;, &quot;-&quot;, right_parens)\nprint(secondCorrectFormat)<\/pre><\/div>\n\n\n\n<p>It\u2019s that simple! There is nothing special you have to add or packages you need to download. It\u2019s already built-in as long as you import it.&nbsp;<br><\/p>\n\n\n\n<p>The fun comes from how to use the different methods that are available to us in the re module.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>re.search(regex, str, flags=0)<\/strong><\/h3>\n\n\n\n<p>Use the search function when you want to apply a regex pattern to a string to see if the pattern is contained in the string. This method will take a regular expression pattern and match it anywhere in the string.<br><\/p>\n\n\n\n<p>If the regex pattern is NOT contained in the string, the interpreter will return <code>None<\/code>. If there is a match, a <code>Match<\/code> object will be returned that will contain some information about it.&nbsp;<\/p>\n\n\n\n<p>The <code>Match<\/code> object has a property and two methods that can be used to retrieve information about the match:<\/p>\n\n\n\n<p><code>match_obj.span()<\/code> a method that returns a tuple containing the start and end positions of the match (the end position is inclusive).<\/p>\n\n\n\n<p><code>match_obj.string<\/code> a property that returns the string passed into the function.<\/p>\n\n\n\n<p><code>match_obj.group()<\/code> a method that returns the part of the string where there was a match.<\/p>\n\n\n\n<p><code>match_obj<\/code> here will be replaced with the variable you assign to the result of your <code>re.search()<\/code> method. Here is an example using each method and property:&nbsp;<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>import re\n \nstring = &quot;The quick brown fox jumped over the lazy dog&quot;\n \nresult = re.search(&quot;q.+k\\s&quot;, string) # this is the match object if it returns a positive result. It will return a NoneType object otherwise\nprint(result.span(), &quot;&lt;== This is the tuple containing the span of indexes the result is in&quot;)\nprint(result.string, &quot;&lt;== This is the original string &quot;)\nprint(result.group(), &quot;&lt;== This is the group of characters that match our regex pattern&quot;)<\/pre><\/div>\n\n\n\n<p>The main thing to remember here is that the <code>Match<\/code> object in Python has methods and&nbsp; properties.<br><\/p>\n\n\n\n<p>We use the property string to access the original string we tested the regex against, the span method to access the indexes, and the group method to get the actual matched result.&nbsp;<br><\/p>\n\n\n\n<p>There are other methods and properties that can be referenced in the Python docs, but these are good to get you started.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Optional arguments:&nbsp; flags<\/h4>\n\n\n\n<p>There is an optional argument you can use with the <code>re<\/code> module\u2019s search method as well. To use it assign the <code>flags<\/code> parameter to a list of the flags you would like to use. The default is set to 0. Some of the more popular options include:&nbsp;<br><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>re.DEBUG<\/li><\/ul>\n\n\n\n<p>This flag will display debug information about the compiled regex if needed.&nbsp;<\/p>\n\n\n\n<p><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>re.I<br>re.IGNORECASE<\/li><\/ul>\n\n\n\n<p>Case insensitive matching. This will ignore the case of the characters passed into sets or literal characters so that both capital- and lower- case letters will match.<br><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>re.M<br>re.MULTILINE<\/li><\/ul>\n\n\n\n<p>Multiline mode. Allows beginning of line and end of line regex metacharacters to be used in multiline strings. Otherwise it would just look at the beginning and the end of the string. Without the multiline flag, the regex engine considers the string one line.<br><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>re.S<br>re.DOTALL<\/li><\/ul>\n\n\n\n<p>This flag tells the regex engine that the dot character will match any character. The default behavior is for the dot character to match every character except a newline character.&nbsp;<br><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>re.X<br>re.VERBOSE<\/li><\/ul>\n\n\n\n<p>The verbose flag allows you to add comments to your regular expression to break down your expression and comment on its purpose. This may help immensely as you are learning regex in Python.&nbsp;<br><\/p>\n\n\n\n<p>This code snippet takes the example from above and incorporates some of the flags listed above. When using more than one flag, use the bitwise | operator in between each as a separator.<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>import re\n \nstring = &quot;&quot;&quot;The\nquick brown\nfox jumped over the lazy dog\n&quot;&quot;&quot;\n \nresult = re.search(&quot;&quot;&quot;\n          \t\t  ^ # beginning of line\n                   Q # literal character 'Q'\n                   . # dot ==&gt; any character\n                   + # quantifier == more than 1\n                   k # literal character 'k'\n                   \\s # special character '\\s'\n                   &quot;&quot;&quot;, string, flags=re.IGNORECASE | re.M | re.VERBOSE)\n                  \n                  \n                  \n                  \nif result:                                \nprint(result.span(), &quot;&lt;== This is the tuple containing the span of indexes the result is in&quot;)\nprint(result.string, &quot;&lt;== This is the original string &quot;)\nprint(result.group(), &quot;&lt;== This is the group of characters that match our regex pattern&quot;)\nelse:\n  \tprint(result)<\/pre><\/div>\n\n\n\n<p>Take the time to notice how the flags help us in testing our string. Try taking the flags parameter out. Does the string return a result? If not, what does it return as a result?&nbsp;<br><\/p>\n\n\n\n<p>Remember that the search method on the re module takes in the regex, the string to be searched, and an optional flags parameter. A truthy value will return a Match Object that has properties and methods associated with it. A falsy value returns a NoneType object.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>re.match(regex, str, flags=0)<\/strong><\/h3>\n\n\n\n<p>Use the&nbsp; match method when you are looking to match a regex pattern to the beginning of the string. If you want to match the regex pattern anywhere in the string, use the search method above instead.&nbsp;<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>import re\n \nstring = &quot;&quot;&quot;The\nquick brown\nfox jumped over the lazy dog\n&quot;&quot;&quot;\n \nprint(re.match(&quot;quick&quot;, string, flags=re.IGNORECASE | re.MULTILINE))\nprint(re.search(&quot;quick&quot;, string, flags=re.IGNORECASE | re.MULTILINE))<\/pre><\/div>\n\n\n\n<p>When looking at this code in a Python interpreter, you will see that the first method, <code>match()<\/code>, will not return a <code>Match<\/code> object, but instead <code>None<\/code>. The second allows for looking inside the string and will return a <code>Match<\/code> object. This is because the <code>match()<\/code> method only looks at the beginning of a string, even if the <code>re.MULTILINE<\/code> flag is raised.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>re.sub(findRegex, replaceWith, str, count=0, flags=0)<\/strong><\/h3>\n\n\n\n<p>The <code>sub()<\/code> method in the re module takes a regex, finds the leftmost match in a string, and replaces it with something else. It repeats the same operation for the number of times indicated in the count parameter. If there is no count parameter or it is set to zero, all occurrences will be replaced.&nbsp;<br><\/p>\n\n\n\n<p>At the beginning of this article we talked about reformatting a phone number. Let\u2019s take a look at how to do that here:&nbsp;<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>import re\n \nphone = &quot;555 555 5555&quot;\ncorrectFormat = re.sub(&quot;\\s&quot;, &quot;-&quot;, phone)\nprint(correctFormat) # 555-555-5555<\/pre><\/div>\n\n\n\n<p>This takes our phone string, finds all occurrences of whitespace, and replaces it with a dash. In this case, we end up with <code>555-555-5555<\/code>.<\/p>\n\n\n\n<p>Let\u2019s try a more difficult reformat:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>import re\n \nphone = &quot;555 555 5555&quot;\n \nleft_parens = re.sub(&quot;^&quot;, &quot;(&quot;, phone)\nright_parens = re.sub(&quot;\\s&quot;, &quot;)&quot;, left_parens, 1)\nsecondCorrectFormat = re.sub(&quot;\\s&quot;, &quot;-&quot;, right_parens)\nprint(secondCorrectFormat) # (555)555-5555<\/pre><\/div>\n\n\n\n<p>In this example, we go to the beginning of the line to add an open parentheses and assign that new string to a variable.&nbsp;<\/p>\n\n\n\n<p>We then take that newly assigned variable (<code>left_parens<\/code>) and use it to perform the same operation on finding the next available whitespace character to replace it with a close parentheses. This is assigned to <code>right_parens<\/code>.&nbsp;<\/p>\n\n\n\n<p>Finally, we take the <code>right_parens<\/code> variable and use it to perform the same operation on the final whitespace character to replace it with a dash.&nbsp;<\/p>\n\n\n\n<p>This will give us <code>(555)555-5555<\/code>.&nbsp;<\/p>\n\n\n\n<p>To recap, the <code>sub()<\/code> method takes in a regex pattern, a replacement string or function, the actual string we want to perform the <code>sub()<\/code> on, and a count. If we don\u2019t provide a count, it will perform the replacements on all occurrences. It returns the new string with the substitutions performed.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Regular Expressions are a way to validate data or to search and replace characters in our strings. Regex consists of metacharacters, quantifiers, and literal characters that can be used to test our strings to see if it passes a validation test or to search and replace Matches.&nbsp;<br><\/p>\n\n\n\n<p>Regex can be a little overwhelming at first, but once you get it, it\u2019s a little bit like riding a bike.&nbsp; It\u2019ll be in the back of your memory and super easy to pick up again.<br><\/p>\n\n\n\n<p>When you feel you have a handle on what comes up in this article, take a look at the Python docs to see what else can be done with regular expressions. Definitely take a look at the compile and split methods.&nbsp;<br><\/p>\n\n\n\n<p>Happy regexing!<\/p>\n","protected":false},"excerpt":{"rendered":"As a new developer, regular expressions (or regex as it\u2019s commonly known), can be daunting because of the strange, unfamiliar syntax: &nbsp;&nbsp;&nbsp; ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$ When I was a new developer and saw the expression above for the first time, I remember my first thought was \u201csay what? I\u2019m never going to be able to learn that&hellip;","protected":false},"author":77,"featured_media":28742,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[16578],"tags":[],"class_list":{"0":"post-28741","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"Python","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":null,"is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.0 (Yoast SEO v27.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Python Regex: A Complete Guide<\/title>\n<meta name=\"description\" content=\"In this article about Python-flavored regex on Career Karma, learn about the components that make up a regular expression, as well as methods and properties to use when testing strings with regex patterns!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/python-regex\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Regex: An Introduction to Using Regular Expressions\" \/>\n<meta property=\"og:description\" content=\"In this article about Python-flavored regex on Career Karma, learn about the components that make up a regular expression, as well as methods and properties to use when testing strings with regex patterns!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/python-regex\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2021-01-26T21:55:23+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-07-20T15:35:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2021\/01\/photo-1511619726879-d4974d118c05.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1020\" \/>\n\t<meta property=\"og:image:height\" content=\"765\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Christina Kopecky\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Christina Kopecky\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"17 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-regex\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-regex\/\"},\"author\":{\"name\":\"Christina Kopecky\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e\"},\"headline\":\"Python Regex: An Introduction to Using Regular Expressions\",\"datePublished\":\"2021-01-26T21:55:23+00:00\",\"dateModified\":\"2022-07-20T15:35:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-regex\/\"},\"wordCount\":2742,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-regex\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2021\/01\/photo-1511619726879-d4974d118c05.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-regex\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-regex\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/python-regex\/\",\"name\":\"Python Regex: A Complete Guide\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-regex\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-regex\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2021\/01\/photo-1511619726879-d4974d118c05.jpg\",\"datePublished\":\"2021-01-26T21:55:23+00:00\",\"dateModified\":\"2022-07-20T15:35:05+00:00\",\"author\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e\"},\"description\":\"In this article about Python-flavored regex on Career Karma, learn about the components that make up a regular expression, as well as methods and properties to use when testing strings with regex patterns!\",\"breadcrumb\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-regex\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-regex\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-regex\/#primaryimage\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2021\/01\/photo-1511619726879-d4974d118c05.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2021\/01\/photo-1511619726879-d4974d118c05.jpg\",\"width\":1020,\"height\":765,\"caption\":\"close up photo of R and E, painted red, but with paint peeling off.\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-regex\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\/\/careerkarma.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\/\/careerkarma.com\/blog\/python\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python Regex: An Introduction to Using Regular Expressions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\",\"url\":\"https:\/\/careerkarma.com\/blog\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/careerkarma.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e\",\"name\":\"Christina Kopecky\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/image-3-150x150.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/image-3-150x150.jpg\",\"caption\":\"Christina Kopecky\"},\"description\":\"Christina is an experienced technical writer, covering topics as diverse as Java, SQL, Python, and web development. She earned her Master of Music in flute performance from the University of Kansas and a bachelor's degree in music with minors in French and mass communication from Southeast Missouri State. Prior to joining the Career Karma team in June 2020, Christina was a teaching assistant, team lead, and section lead at Lambda School, where she led student groups, performed code and project reviews, and debugged problems for students. Christina's technical content is featured frequently in publications like Codecademy, Repl.it, and Educative.\",\"sameAs\":[\"http:\/\/www.linkedin.com\/in\/cmvnk\"],\"url\":\"https:\/\/careerkarma.com\/blog\/author\/christina-kopecky\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python Regex: A Complete Guide","description":"In this article about Python-flavored regex on Career Karma, learn about the components that make up a regular expression, as well as methods and properties to use when testing strings with regex patterns!","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:\/\/careerkarma.com\/blog\/python-regex\/","og_locale":"en_US","og_type":"article","og_title":"Python Regex: An Introduction to Using Regular Expressions","og_description":"In this article about Python-flavored regex on Career Karma, learn about the components that make up a regular expression, as well as methods and properties to use when testing strings with regex patterns!","og_url":"https:\/\/careerkarma.com\/blog\/python-regex\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2021-01-26T21:55:23+00:00","article_modified_time":"2022-07-20T15:35:05+00:00","og_image":[{"width":1020,"height":765,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2021\/01\/photo-1511619726879-d4974d118c05.jpg","type":"image\/jpeg"}],"author":"Christina Kopecky","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"Christina Kopecky","Est. reading time":"17 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/python-regex\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/python-regex\/"},"author":{"name":"Christina Kopecky","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e"},"headline":"Python Regex: An Introduction to Using Regular Expressions","datePublished":"2021-01-26T21:55:23+00:00","dateModified":"2022-07-20T15:35:05+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-regex\/"},"wordCount":2742,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-regex\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2021\/01\/photo-1511619726879-d4974d118c05.jpg","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/python-regex\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/python-regex\/","url":"https:\/\/careerkarma.com\/blog\/python-regex\/","name":"Python Regex: A Complete Guide","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-regex\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-regex\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2021\/01\/photo-1511619726879-d4974d118c05.jpg","datePublished":"2021-01-26T21:55:23+00:00","dateModified":"2022-07-20T15:35:05+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e"},"description":"In this article about Python-flavored regex on Career Karma, learn about the components that make up a regular expression, as well as methods and properties to use when testing strings with regex patterns!","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/python-regex\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/python-regex\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/python-regex\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2021\/01\/photo-1511619726879-d4974d118c05.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2021\/01\/photo-1511619726879-d4974d118c05.jpg","width":1020,"height":765,"caption":"close up photo of R and E, painted red, but with paint peeling off."},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/python-regex\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/careerkarma.com\/blog\/python\/"},{"@type":"ListItem","position":3,"name":"Python Regex: An Introduction to Using Regular Expressions"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e","name":"Christina Kopecky","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/image-3-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/image-3-150x150.jpg","caption":"Christina Kopecky"},"description":"Christina is an experienced technical writer, covering topics as diverse as Java, SQL, Python, and web development. She earned her Master of Music in flute performance from the University of Kansas and a bachelor's degree in music with minors in French and mass communication from Southeast Missouri State. Prior to joining the Career Karma team in June 2020, Christina was a teaching assistant, team lead, and section lead at Lambda School, where she led student groups, performed code and project reviews, and debugged problems for students. Christina's technical content is featured frequently in publications like Codecademy, Repl.it, and Educative.","sameAs":["http:\/\/www.linkedin.com\/in\/cmvnk"],"url":"https:\/\/careerkarma.com\/blog\/author\/christina-kopecky\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/28741","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/77"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=28741"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/28741\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/28742"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=28741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=28741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=28741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}