{"id":909,"date":"2014-11-18T06:15:01","date_gmt":"2014-11-18T06:15:01","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=909"},"modified":"2015-08-17T06:08:25","modified_gmt":"2015-08-17T06:08:25","slug":"regular-expression-to-match-sets-of-numbers-that-are-not-equal-nor-reversed","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/stack-overflow\/regular-expression-to-match-sets-of-numbers-that-are-not-equal-nor-reversed\/","title":{"rendered":"Regular expression to match sets of numbers that are not equal nor reversed"},"content":{"rendered":"<p><a href=\"http:\/\/stackoverflow.com\/users\/534755\/nikola\"><img loading=\"lazy\" decoding=\"async\" title=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" src=\"http:\/\/stackoverflow.com\/users\/flair\/534755.png\" rel=\"lightbox[909]\" alt=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" width=\"208\" height=\"58\" \/><\/a><br \/>\nI&#8217;m a big fan of <a href=\"http:\/\/stackoverflow.com\/\">Stack Overflow<\/a> and I tend to contribute regularly (am currently in the <a href=\"http:\/\/stackexchange.com\/leagues\/1\/alltime\/stackoverflow\/2008-07-31\/534755?sort=reputationchange#534755\">top 0.X%<\/a>).\u00a0In this category (<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/category\/stack-overflow\/\">stackoverflow<\/a>)\u00a0of posts I will will be posting my top rated questions and answers. This, btw, is allowed as explained in the meta thread <a href=\"http:\/\/meta.stackoverflow.com\/questions\/266971\/can-i-post-so-questions-and-answers-in-a-personal-blog\/266973\">here<\/a>.<\/p>\n<p>My <a href=\"http:\/\/stackoverflow.com\/questions\/5258471\/regular-expression-to-match-sets-of-numbers-that-are-not-equal-nor-reversed\">quesiton<\/a> was:<\/p>\n<p style=\"color: #000000;\">I am referring as a follow up on this question here:\u00a0<a style=\"color: #4a6b82;\" href=\"http:\/\/stackoverflow.com\/questions\/5257685\/regular-expression-to-match-two-numbers-that-are-not-equal\">Regular expression to match two numbers that are not equal<\/a><\/p>\n<p style=\"color: #000000;\">Now my other question scenario is this:<\/p>\n<pre class=\"lang:default decode:true \">P121324 - T  \r\nP1212 - F - we got this covered in the message on link above (no same \"sets\")\r\nP1221 - F - now new restriction -not even the reversed digits 12-21<\/pre>\n<p>But, the problem now is that the P string can be very long! &#8211; like this:<\/p>\n<pre class=\"lang:default decode:true \">P121315162324<\/pre>\n<p>please notice this is ok since the &#8220;sets&#8221; are:<\/p>\n<pre class=\"lang:default decode:true \">12131415162324<\/pre>\n<p>Now, I could make this in the code (PHP) by checking if there are repetitions, but I was wondering if this can be done with single regex command?<\/p>\n<p style=\"color: #000000;\"><span style=\"line-height: 1.5;\">The answer, by <a style=\"color: #4a6b82;\" href=\"http:\/\/stackoverflow.com\/users\/61974\/mark-byers\">Mark Byers<\/a>,\u00a0was:<\/span><\/p>\n<blockquote>\n<p style=\"color: #000000;\">Try this:<\/p>\n<pre class=\"lang:default decode:true \">^P(?:([0-9])(?!\\1)([0-9])(?!(?:..)*(?:\\1\\2|\\2\\1)))*$<\/pre>\n<p>If you want the digits to be restricted to [1-6] like in your previous question then change [0-9] to [1-6].<\/p>\n<p style=\"color: #000000;\">See it working online:\u00a0<a style=\"color: #4a6b82;\" href=\"http:\/\/www.rubular.com\/r\/AGlyBYW9KW\" rel=\"nofollow\">rubular<\/a><\/p>\n<hr style=\"color: #dddddd;\" \/>\n<p style=\"color: #000000;\">Here is a breakdown of the regular expression:<\/p>\n<pre class=\"lang:default decode:true\">^          Start of string\/line.\r\nP          Literal P\r\n(?:&lt;snip&gt;) Non-capturing group that matches a distinct pair of digits. See below.\r\n*          Zero or more pairs (use + if you want to require at least one pair).\r\n$          End of string\/line.\r\n<\/pre>\n<p style=\"color: #000000;\">Explanation of\u00a0<code>([0-9])(?!\\1)([0-9])(?!(?:..)*(?:\\1\\2|\\2\\1))<\/code>\u00a0&#8211; match one pair:<\/p>\n<pre style=\"color: #000000;\">([0-9])    Match and capture the first digit. Later refered to as \\1.\r\n(?!\\1)     Negative lookahead. The next character must not be the same as \\1.\r\n([0-9])    Match and capture a digit. Later refered to as \\2.\r\n(?!&lt;snip&gt;) Negative lookahead. Check that the pair doesn't occur again.\r\n<\/pre>\n<p style=\"color: #000000;\">Explanation of\u00a0<code>(?:..)*(?:\\1\\2|\\2\\1)<\/code>\u00a0&#8211; try to find the same pair again:<\/p>\n<pre style=\"color: #000000;\" class=\"\">(?:..)*       Match any number of pairs.\r\n(?:\\1\\2|\\2\\1) Match either \\1\\2 or \\2\\1.<\/pre>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m a big fan of Stack Overflow and I tend to contribute regularly (am currently in the top 0.X%).\u00a0In this category (stackoverflow)\u00a0of posts I will will be posting&hellip;<\/p>\n","protected":false},"author":1,"featured_media":609,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-909","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-stack-overflow"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/909","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=909"}],"version-history":[{"count":4,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/909\/revisions"}],"predecessor-version":[{"id":1701,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/909\/revisions\/1701"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/609"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=909"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=909"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=909"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}