{"id":1657,"date":"2011-07-20T17:22:35","date_gmt":"2011-07-20T17:22:35","guid":{"rendered":"http:\/\/doanduyhai.wordpress.com\/?p=1"},"modified":"2011-07-20T17:22:35","modified_gmt":"2011-07-20T17:22:35","slug":"regular-expression-part1","status":"publish","type":"post","link":"https:\/\/www.doanduyhai.com\/blog\/?p=1657","title":{"rendered":"Advanced Regular Expressions: part 1 &#8211; look ahead\/look behind"},"content":{"rendered":"<p>We are going to dig into one of the most powerful so-called &#8220;language&#8221; to process text, the Regular Expressions (RegExp).<br \/>\n&nbsp;<br \/>\nIn this series of posts I will only discuss about advanced features of the RegExp. If you are not familiar with it already, I will advise dropping an eye <a href=\"http:\/\/www.regular-expressions.info\/\" target=\"_blank\">here<\/a>. <\/p>\n<p>Today I start with the<strong> look ahead\/look behind<\/strong> construct.<br \/>\n<!--more--><\/p>\n<p>Most of us are quite familiar with the very common quantifier characters coming from the RegExp: star (*) and dot (.). With these two modifiers combined with some clever pattern, we can cover almost 90% of common use cases.<br \/>\nBut sometimes we need to go further into the advanced features because the task requirements can not be done easily with basic regexps.<\/p>\n<p>As typographic convention<\/p>\n<ul>\n<li>all regexp patterns will be put in italic and large font:\u00a0 <em><span style=\"font-size:large;\">[a-zA-Z]+<\/span><\/em><\/li>\n<li>all\u00a0 strings to be matches underlined:\u00a0 <span style=\"text-decoration:underline;\">This is a text to be matched<\/span><\/li>\n<li>when necessary I will indicate the current matching position in the string by a \u2191 symbol<\/li>\n<\/ul>\n<p style=\"padding-left:30px;\">\u00a0<span style=\"text-decoration:underline;\">This is\u2191 a text to be matched<\/span><\/p>\n<h3>I\u00a0 Look ahead\/look behind<\/h3>\n<p style=\"text-align:justify;\">One day I had a requirement to do some input field validation for list of email addresses.<\/p>\n<p style=\"text-align:justify;\">We had an input text field in the form, and we expected the user to provide a list of emails separated by a comma (,).<\/p>\n<p style=\"text-align:justify;\">For the sake of simplicity, let&#8217;s say that the pattern for a valid email is: <em><span style=\"font-size:large;\">[-_a-zA-Z0-9]+@[-_a-zA-Z0-9]+.[a-z]{3}<\/span><\/em><\/p>\n<ul>\n<li><em><span style=\"font-size:large;\">[-_a-zA-Z0-9]+<\/span><\/em> : one or more alphanumeric characters, including underscore ( _ ) and dash (-)<\/li>\n<li><em><span style=\"font-size:large;\"> .<\/span><\/em> : dot (.) character. The slash here is to escape the special meaning of the dot (.) as &#8220;<em>any character<\/em>&#8220;<\/li>\n<li><em><span style=\"font-size:large;\">[a-z]{3}<\/span><\/em> : exactly 3 alphabetic characters. Numbers are not allowed here.<\/li>\n<\/ul>\n<p style=\"text-align:justify;\">This pattern is indeed very simplistic, the real regexp defined by the 3W consortium to validate all possible email addresses is very complex.<\/p>\n<p style=\"text-align:justify;\">The pattern above only deals with email address. We&#8217;ve not yet looked at the comma as separator.<\/p>\n<p style=\"text-align:justify;\">One na\u00efve approach could be:\u00a0<em><span style=\"font-size:large;\"> (?:[-_a-zA-Z0-9]+@[-_a-zA-Z0-9]+.[a-z]{3},)+<\/span><\/em><\/p>\n<ul>\n<li>first is the pattern for the email address<\/li>\n<li>followed by the comma <em><span style=\"font-size:large;\">,<\/span><\/em><\/li>\n<li>both patterns are inside a <em>non-capturing<\/em> group\u00a0\u00a0<em><span style=\"font-size:large;\">(?:\u00a0 )<\/span><\/em><\/li>\n<li>followed by the <em><span style=\"font-size:large;\">+<\/span><\/em> quantifier meaning that this group can occur more than once<\/li>\n<\/ul>\n<p style=\"text-align:justify;\">This pattern will work most of the time and match correctly everything, including <span style=\"text-decoration:underline;\">john-smith@gmail.com,<\/span>\u00a0 which we do not want. How to get rid of the last comma ?<\/p>\n<p style=\"text-align:justify;\">Ideally we want to match an email address followed by a comma, one time or more, but the last group should contain only email address, without the comma.<\/p>\n<p style=\"text-align:justify;\">In other words, the text string should <strong>NOT<\/strong> finish by a comma, e.g. the comma is NOT allowed if it is the last character of the string.<\/p>\n<p style=\"text-align:justify;\">For this we can use the negative look-ahead construct:\u00a0<em><span style=\"font-size:large;\">pattern1(?!look-ahead)<\/span><\/em><\/p>\n<p style=\"text-align:justify;\">This construct means: match <em>pattern1<\/em> only if <em>pattern1<\/em> is <strong>NOT<\/strong> followed by <em>pattern2<\/em><\/p>\n<p style=\"text-align:justify;\">So we will have <em><span style=\"font-size:large;\">(?:[-_a-zA-Z0-9]+@[-_a-zA-Z0-9]+\\.[a-z]{3}<strong>,(?!$)<\/strong>)+<\/span><\/em><\/p>\n<p style=\"text-align:justify;\">But still this doesn&#8217;t work. Why ?<\/p>\n<p style=\"text-align:justify;\">By appending <em><span style=\"font-size:large;\">,(?!$)<\/span><\/em> we slightly changed the meaning of the pattern. Now it means one or more group of\u00a0 (a valid email address, followed by a non-terminal comma). In plain English this is quite obvious that it should work.<\/p>\n<p style=\"text-align:justify;\">Let&#8217;s take the following string:\u00a0 <span style=\"text-decoration:underline;\">john-smith@gmail.com,adam-smith@yahoo.net<\/span><\/p>\n<p style=\"text-align:justify;\">The first part <span style=\"text-decoration:underline;\">john-smith@gmail.com,<\/span> surely matches the pattern\u00a0<em><span style=\"font-size:large;\">[-_a-zA-Z0-9]+@[-_a-zA-Z0-9]+\\.[a-z]{3},(?!$)<\/span><\/em>. However, the last part <span style=\"text-decoration:underline;\">adam-smith@yahoo.net<\/span> won&#8217;t match anything because the only allowed matching pattern is &#8220;<em>mail address,&#8221;<\/em><\/p>\n<p style=\"text-align:justify;\">The mistakes here is the <em><span style=\"font-size:large;\">,(?!$)<\/span><\/em>. Indeed it means a non-terminal comma, and the non-terminal comma IS NOT OPTIONAL. The string must include the non-terminal comma otherwise the whole match will fail.<\/p>\n<p style=\"text-align:justify;\">To make the non-terminal comma optional, we simply wrap it inside a non-capturing group with the <em><span style=\"font-size:large;\">?<\/span><\/em> (zero or one) quantifier: <em><span style=\"font-size:large;\">(?:,(?!$))?<\/span><\/em><\/p>\n<p style=\"text-align:justify;\">So the final regexp is: <em><span style=\"font-size:large;\">(?:[-_a-zA-Z0-9]+@[-_a-zA-Z0-9]+\\.[a-z]{3}(?:,(?!$))?)+<\/span><\/em><\/p>\n<p style=\"text-align:justify;\">In the previous example<\/p>\n<ul>\n<li><span style=\"text-decoration:underline;\">\u00a0john-smith@gmail.com,<\/span> will match <em><span style=\"font-size:large;\">[-_a-zA-Z0-9]+@[-_a-zA-Z0-9]+\\.[a-z]{3},(?!$)<\/span><\/em><\/li>\n<li><span style=\"text-decoration:underline;\"><span style=\"text-align:justify;\">adam-smith@yahoo.net<\/span><\/span> will match <em><span style=\"font-size:large;\">[-_a-zA-Z0-9]+@[-_a-zA-Z0-9]+\\.[a-z]{3}<\/span><\/em> without the non-terminal comma.<\/li>\n<\/ul>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\n\nString text = &amp;quot;john-smith@hotmail.com,brian_adams@yahoo.net&amp;quot;;\n\nPattern pattern = Pattern.compile(&amp;quot;(?:[-_a-zA-Z0-9]+@[-_a-zA-Z0-9]+\\.[a-z]{3}(?:,(?!$))?)+&amp;quot;);\nMatcher matcher = pattern.matcher(text);\nSystem.out.println(&amp;quot; Matches ? &amp;quot; + matcher.matches());\n\n<\/pre>\n<p>The output:<\/p>\n<blockquote><p>\u00a0Matches ? true<\/p><\/blockquote>\n<p>The other constructs, namely positive look-ahead, positive look-behind and negative look-behind work similarly.<\/p>\n<ul>\n<li>Positive look-ahead:\u00a0 <em><span style=\"font-size:large;\">pattern<span style=\"color:#ff0000;\">(?=<\/span>look-ahead<span style=\"color:#ff0000;\">)<\/span><\/span><\/em><\/li>\n<li>Negative look-ahead: <em><span style=\"font-size:large;\">pattern<span style=\"color:#ff0000;\">(?!<\/span>look-ahead<span style=\"color:#ff0000;\">)<\/span><\/span><\/em><\/li>\n<li>Positive look-behind: <em><span style=\"font-size:large;\"><span style=\"color:#ff0000;\">(?&lt;=<\/span>look-behind<span style=\"color:#ff0000;\">)<\/span>pattern<\/span><\/em><\/li>\n<li>Negative look-behind: <em><span style=\"font-size:large;\"><span style=\"color:#ff0000;\">(?&lt;!<\/span>look-behind<span style=\"color:#ff0000;\">)<\/span>pattern<\/span><\/em><\/li>\n<\/ul>\n<p>Recommended readings:<\/p>\n<ul>\n<li><a href=\"http:\/\/www.regular-expressions.info\/lookaround.html\" target=\"_blank\">http:\/\/www.regular-expressions.info\/lookaround.html<\/a><\/li>\n<li><a href=\"http:\/\/oreilly.com\/catalog\/9780596528126\" target=\"_blank\">Mastering Regular Expressions, 3rd edition<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>We are going to dig into one of the most powerful so-called &#8220;language&#8221; to process text, the Regular Expressions (RegExp). &nbsp; In this series of posts I will only discuss about advanced features of the RegExp. If you are not&#8230;<br \/><a class=\"read-more-button\" href=\"https:\/\/www.doanduyhai.com\/blog\/?p=1657\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[12],"tags":[45],"_links":{"self":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1657"}],"collection":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1657"}],"version-history":[{"count":0,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1657\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1657"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1657"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.doanduyhai.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1657"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}