{"id":85,"date":"2009-02-17T12:20:43","date_gmt":"2009-02-17T10:20:43","guid":{"rendered":"http:\/\/dev.flauschig.ch\/wordpress\/?p=85"},"modified":"2009-05-16T09:56:52","modified_gmt":"2009-05-16T07:56:52","slug":"regex-tips","status":"publish","type":"post","link":"http:\/\/dev.flauschig.ch\/wordpress\/?p=85","title":{"rendered":"Regex Tips"},"content":{"rendered":"<p>Since I always forget some important things about Regex, I&#8217;ll write it down here:<\/p>\n<p><strong>Greedy vs Non-Greedy<\/strong><br \/>\nIf you have something like &#8220;hello|world|!&#8221; and you want to get only the first part before the first Delimiter (which obviously is &#8220;|&#8221;) your first try could look like:<br \/>\n<code>(.*)\\|<\/code><br \/>\nThat one would match &#8220;hello|world&#8221; since by default Regex is Greedy (aka. &#8220;take as much as possible&#8221;).<\/p>\n<p>The old-fashioned way was:<br \/>\n<code>([^\\|]+)<\/code><br \/>\nThis one does match unless the &#8220;|&#8221; Sign is found.<\/p>\n<p>Now Regex has a much nicer solution:<br \/>\n<code>(.*?)\\|<\/code><br \/>\nThe added &#8220;?&#8221; makes it Non-Greedy (aka. &#8220;take as less as possible)<\/p>\n<p><strong>Non-Capturing Group<\/strong><br \/>\nSometimes you want to create a Group which is not captured. Just begin those Groups with an &#8220;?:&#8221; and it will not get captured.<br \/>\nExample:<br \/>\n<code>(?:abc)<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since I always forget some important things about Regex, I&#8217;ll write it down here: Greedy vs Non-Greedy If you have something like &#8220;hello|world|!&#8221; and you want to get only the first part before the first Delimiter (which obviously is &#8220;|&#8221;) your first try could look like: (.*)\\| That one would match &#8220;hello|world&#8221; since by default &hellip; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[15,3],"tags":[34,36,35,33],"class_list":{"0":"entry","1":"post","2":"publish","3":"author-roemer","4":"post-85","6":"format-standard","7":"category-general","8":"category-programming","9":"post_tag-greedy","10":"post_tag-group","11":"post_tag-non-capturing","12":"post_tag-regex"},"acf":[],"views":1670,"_links":{"self":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/85"}],"collection":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=85"}],"version-history":[{"count":0,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/85\/revisions"}],"wp:attachment":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=85"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=85"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=85"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}