{"id":17203,"date":"2009-08-06T10:00:00","date_gmt":"2009-08-06T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2009\/08\/06\/the-great-thing-about-regular-expression-engines-is-that-there-are-so-many-to-choose-from\/"},"modified":"2009-08-06T10:00:00","modified_gmt":"2009-08-06T10:00:00","slug":"the-great-thing-about-regular-expression-engines-is-that-there-are-so-many-to-choose-from","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20090806-00\/?p=17203\/","title":{"rendered":"The great thing about regular expression engines is that there are so many to choose from"},"content":{"rendered":"<p>Back in the days before perl ruled the earth, regular expressions were one of those weird niche features, one of those things that everybody reimplements when they need it. If you look at the old unix tools, you&#8217;ll see that even then, there were three different regular expression engines with different syntax. You had <code>grep<\/code>, <code>egrep<\/code>, and <code>vi<\/code>. Probably more.\n The <code>grep<\/code> regular expression language supported character classes, the dot wildcard, the asterisk operator, the start and end anchors, and grouping. No plus operator, no question mark, no alternation, no repetition counts. The <code>egrep<\/code> program added support for plus, question mark, and alternation. Meanwhile, somebody went back and added repetition counts to <code>grep<\/code> but didn&#8217;t add them to <code>vi<\/code>; somebody else added the <code>\\&lt;<\/code> and <code>\\&gt;<\/code> metacharacters to <code>vi<\/code> but didn&#8217;t add them to <code>sed<\/code>. POSIX added repetition counts to <code>awk<\/code> but changed the notation from <code>\\{n,m\\}<\/code> to <code>{n,m}<\/code>. And so on.\n No two programs use the same regular expression language, but they overlap sufficiently that you can often get by with the common subset and not have to worry about which particular flavor you&#8217;re up against.<\/p>\n<p> Until you wander into the places where they differ. <\/p>\n<blockquote class=\"m\">\n<p> <b>From<\/b>: John Jones<br \/> <b>Subject<\/b>: Problem with regular expression <\/p>\n<p> I&#8217;m trying to write a regular expression to match blah blah blah. <\/p>\n<\/blockquote>\n<blockquote class=\"m\">\n<p> <b>From<\/b>: Jane Smith<br \/> <b>Subject<\/b>: RE: Problem with regular expression <\/p>\n<p> I think this will match what you want: ^Z@1&amp;*B*!34 <\/p>\n<\/blockquote>\n<p> I just ran my hand randomly over the keyboard to generate that fake regular expression. The scary thing is, at first glance, it is not obviously <i>not<\/i> a regular expression! <\/p>\n<blockquote class=\"m\">\n<p> <b>From<\/b>: Chris Brown<br \/> <b>Subject<\/b>: RE: Problem with regular expression <\/p>\n<p> Try $)(#$C)*# <\/p>\n<\/blockquote>\n<blockquote class=\"m\">\n<p> <b>From<\/b>: John Smith<br \/> <b>Subject<\/b>: RE: Problem with regular expression <\/p>\n<p> Thanks, everybody, for your suggestions, but I can&#8217;t get any of them to work. For example, I can&#8217;t get any of them to match against this string: blah blah blah blah. <\/p>\n<\/blockquote>\n<p> At this point, people chimed in with other suggestions, confirming that John doubled the backslashes, that sort of thing. John posted his test program, and then the reason was obvious. <\/p>\n<blockquote class=\"m\">\n<p> <b>From<\/b>: Jane Smith<br \/> <b>Subject<\/b>: RE: Problem with regular expression <\/p>\n<p> Oh, <a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2006\/05\/22\/603788.aspx#603889\"> you&#8217;re using <code>CAtlRegExp<\/code><\/a>. In that class, <code>\\w<\/code> doesn&#8217;t match a single character; <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/k3zs4axe(VS.80).aspx\"> it matches an entire word<\/a>. You want to use <code>\\a<\/code> instead. <\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Back in the days before perl ruled the earth, regular expressions were one of those weird niche features, one of those things that everybody reimplements when they need it. If you look at the old unix tools, you&#8217;ll see that even then, there were three different regular expression engines with different syntax. You had grep, [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-17203","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Back in the days before perl ruled the earth, regular expressions were one of those weird niche features, one of those things that everybody reimplements when they need it. If you look at the old unix tools, you&#8217;ll see that even then, there were three different regular expression engines with different syntax. You had grep, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/17203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=17203"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/17203\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=17203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=17203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=17203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}