
    
        
        
        
            
{"version":"https:\/\/jsonfeed.org\/version\/1","title":"mathspp.com feed","home_page_url":"https:\/\/mathspp.com\/blog\/tags\/grammars","feed_url":"https:\/\/mathspp.com\/blog\/tags\/grammars.json","description":"Stay up-to-date with the articles on mathematics and programming that get published to mathspp.com.","author":{"name":"Rodrigo Gir\u00e3o Serr\u00e3o"},"items":[{"title":"Finding all strings a regular expression can find","date_published":"2017-11-20T00:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/regex-printer","url":"https:\/\/mathspp.com\/blog\/regex-printer","content_html":"<p>A <a href=\"https:\/\/en.wikipedia.org\/wiki\/Regular_expression\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">regular expression<\/a>, without much rigor, is a very compact way of representing several different strings. Given a regular expression (regex), can I find out all the strings the regex can find?<\/p>\n\n<figure class=\"image-caption\"><img title=\"Photo by Aaron Burden on Unsplash\" alt=\"page of a book with a paragraph highlighted\" src=\"\/user\/pages\/02.blog\/regex-printer\/hightlighted-book.jpg\"><figcaption class=\"\">Photo by Aaron Burden on Unsplash<\/figcaption><\/figure><p>One common use of regular expressions is to look for strings that have a certain structure in a bigger string (say a text). As an example, the regular expression <code>abc(d|e)<\/code> can be used to look for the strings \"abcd\" and \"abce\", where the character <code>|<\/code> denotes we have to make a choice (see image above). Thus <code>cat|dog<\/code> would match the strings \"cat\" and \"dog\". There are other special symbols that have meanings and purposes.<\/p>\n<p><img alt=\"two screenshots of my program\" src=\"\/user\/pages\/02.blog\/regex-printer\/regex.webp\"><\/p>\n<p>One very interesting question that arises is: given a regular expression, what are the strings matched by it? To answer that question I wrote a small Python program, that I called <code>regexPrinter<\/code>, that prints all strings matched by a given regular expression! In order to manage that task, I chose a subset of the regex syntax that I wanted to be able to print and also decided that whenever a piece of a pattern was infinite, at a given point the program would just print \"...\" to denote that infinity. This way, for any regex given, the program always stops.<\/p>\n<p>The <code>regexPrinter<\/code> supports:<\/p>\n<ul><li>the <code>*<\/code> operator, that denotes that <span class=\"mathjax mathjax--inline\">\\(0\\)<\/span> or more repetitions are to be matched. For example, <code>ah*<\/code> matches \"a\", \"ah\", \"ahh\", ...;<\/li>\n<li>the <code>+<\/code> operator that denotes that <span class=\"mathjax mathjax--inline\">\\(1\\)<\/span> or more repetitions are to be matched. For example, <code>(hue)+<\/code> matches \"hue\", \"huehue\", \"huehuehue\", ...;<\/li>\n<li>the <code>?<\/code> operator that denotes either <span class=\"mathjax mathjax--inline\">\\(0\\)<\/span> or <span class=\"mathjax mathjax--inline\">\\(1\\)<\/span> occurrences of the preceding pattern. For example, <code>woo(hoo)?<\/code> matches \"woo\" and \"woohoo\";<\/li>\n<li>the <code>{a:b}<\/code> operator that matches no less than <span class=\"mathjax mathjax--inline\">\\(a\\)<\/span> and no more than <span class=\"mathjax mathjax--inline\">\\(b\\)<\/span> repetitions of the preceding pattern. As an example, <code>su{1:3}re<\/code> matches the strings \"sure\", \"suure\" and \"suuure\";<\/li>\n<li>the <code>|<\/code> operator that denotes a choice. <code>cat|kat<\/code> matches \"cat\" and \"kat\" and <code>thank(s| you)<\/code> matches \"thanks\" and \"thank you\";<\/li>\n<li>the <code>[]<\/code> denote that only one pattern from the ones given are to be matched. For example <code>[abc]<\/code> matches \"a\", \"b\" and \"c\";<\/li>\n<li>the parentheses <code>()<\/code> that are used to group things. One thing to note is that the quantifiers <code>*+?{:}<\/code> all have higher precedence than string concatenation e.g., <code>ab?<\/code> is interpreted as <code>a(b?)<\/code> and <em>not<\/em> <code>(ab)?<\/code>.<\/li>\n<\/ul><p>Please bear in mind that any character with no special meaning is interpreted literally, except inside the grouping operator <code>[]<\/code>, where every character is interpreted literally. That is, <code>[ab*]<\/code> matches \"a\", \"b\" and \"*\", while <code>b&amp;='<\/code> will match the string \"b&amp;='\".<\/p>\n<p><img alt=\"another screenshot of my program\" src=\"\/user\/pages\/02.blog\/regex-printer\/regex2.webp\"><\/p>\n<p>The code for the program can be found <a href=\"https:\/\/github.com\/RodrigoGiraoSerrao\/projects\/blob\/master\/misc\/regexPrinter.py\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">here<\/a> on GitHub and all it takes is vanilla Python 3 to run. Just run the script and you will be prompted to insert regular expressions. The techniques I used were very, very similar to the ones I used to create <a href=\"https:\/\/mathspp.com\/blog\/creating-programming-language-from-scratch\">my toy programming language<\/a>, as I saw in <a href=\"https:\/\/ruslanspivak.com\/lsbasi-part1\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">this<\/a> blog series. The way I went about writing this...<\/p>","summary":"In this post I explain briefly what a regex is and show something interesting I did with them.","date_modified":"2024-08-13T12:47:46+02:00","tags":["python","grammars","computation theory","parsers","regex"],"image":"\/user\/pages\/02.blog\/regex-printer\/hightlighted-book.jpg"}]}
