Answers.Body: <p>It is important to know that there are two languages involved, each with its own rules and metacharacters.  </p>
  
  <p>Characterclassese is the language of character classes, which are character wildcards.  Its special characters include <code>[ ] ^-</code>.  Every character class stands for a single character.   Literal characters are themselves.  </p>
  
  <p>Regexese is a second language. Its atoms are character classes.  Here are some of its rules</p>
  
  <ul>
  <li>Juxaposition means "and then immediately."</li>
  <li>It has an operator for <code>or</code> which is <code>|</code></li>
  <li>Parentheses are delimiters and can override the order of operations</li>
  <li>There are several postfix multiplicity operators which have higher precedence than juxtaposition. </li>
  <li>Special character classes include <code>.^$</code>  These are metacharacters in the 
  language as well.</li>
  </ul>
  
  <p>It is important to point out the existence of the two layers and how they work together.  "Hedwig," the O'Reilly book on Regexes is great.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-06T17:10:35.067Z
Answers.LastActivityDate: 2017-08-06T17:10:35.067Z
Answers.UserDisplayName: ncmathsadist
Answers.Body: <blockquote>
    <p>Many students find them difficult and intimidating.</p>
  </blockquote>
  
  <p>Then the challenge is to make them fun and engaging and (ideally) interactive. I myself have undertaken the challenge here and there of teaching myself regex, and two resources rose to the top in my own learning: <a href="https://regexone.com/" rel="noreferrer">RegexOne</a> and <a href="https://regexcrossword.com/" rel="noreferrer">Regex Crossword</a>.</p>
  
  <p>RegexOne is great as a lesson-by-lesson tutorial. Students can type and see in real time what tests cases their regex does/does not match. There are 15.5 lessons and 8 practice problems. The practice problems draw from relevant use cases, such as extracting a phone number, trimming white space, or reading a log file.</p>
  
  <p>Regex Crossword mixes regex with logic: students have to fill in a square grid and track how gradually more complex regular expressions interact with each other. (That sounds a bit vague, but an example like <a href="https://regexcrossword.com/challenges/beginner/puzzles/1" rel="noreferrer">this one</a> makes it clear what I mean.) The crosswords tend to have fun themes/solutions. There are many crosswords to solve, including ones with great complexity, so students will have to spend lots of time working at it if they'd like to complete all the puzzles. </p>
  
  <p>When it comes to working in class, it'd be pretty easy to work with some of the smaller crosswords in a non-digital manner. You could put one on the board at the start of every class as a warm-up or print/copy some of your favorite from the site. Maybe even have students work in groups and complete to solve a series of puzzles in the fastest possible time.</p>
  
  <p>Between these two resources, students will at least have a pretty good start to working with regex. </p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-05T22:06:57.487Z
Answers.Text: In a similar way you could create a Jeopardy game. The Jeopardy "answers" are REs. The contestants are given an RE for some category and the first to "ring the bell" gets to reply with a valid match - in the form of a question, of course. I used to keep a set of hotel desk bells for this sort of thing. This only works for deciphering/reading, of course. As you proceed through a category the REs get harder. The categories can be RE types or something more elaborate.
Answers.UserDisplayName: Buffy
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-10T07:30:37.387Z
Answers.Text: A similar activity to RegexOne, without the tutorial text but with larger test suites for each problem, is https://alf.nu/RegexGolf
Answers.UserDisplayName: Peter Taylor
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-05T20:00:22.203Z
Answers.LastActivityDate: 2017-08-05T20:00:22.203Z
Answers.UserDisplayName: Peter
Answers.Body: <p>In Java: <strong>Don't</strong></p>
  
  <p>In programming language <em>X</em>: <strong>Don't</strong></p>
  
  <p>Treat Regular Expression as its own language. Teach regex in a language-agnostic fashion using tools that process regular expressions rather than trying to teach regular expressions <strong>and</strong> their implementation in Java, or any other language. Each language has its own, idiosyncratic, engine and method of use. Different engines implement subsets of the entirety of the regular expression specifications.</p>
  
  <p>As a starting point, use an online version of a regular expression editor. There are two fairly good versions that I'm aware of. First is <a href="https://regex101.com/" rel="nofollow noreferrer" title="Online regular expression editor IDE from Regex101">Regex101</a>, which has a decent IDE style interface, including an live explanation of the current expression, and a token list to choose from. The one feature I like is that it allows you to "Save &amp; Share" the expression, which adds it to a library of expressions available to others. Of course, that library is also available to you, so you can try to find one that's already written to do what you need. One major issue I have with this site is that it still does not implement the entire specification of regular expressions. Specifically, it does not handle If-Then-Else conditionals: <code>(?(subpattern)yes|no)</code> and <code>(?(group)yes|no)</code>.</p>
  
  <p>A second site, which will handle the If-Then-Else conditionals, is <a href="http://regexstorm.net/tester" rel="nofollow noreferrer" title="Online regular expression editor from Regex Storm">Regex Storm</a>. This one produces a nice set of tables for the "output" of the expression, but lacks the collection of tokens, and library of expressions of Regex101. As near as I have been able to determine, this editor implements the entire set of regular expressions available in all engines extant, including left-to-right text. There is also an extensive reference on this site that can be terse at times, but does include examples for most of the complex, or potentially confusing, constructs.</p>
  
  <p>After selecting the editor to use, local, online, etc., the problem now becomes how to work through the "language" of regex. I don't like to reinvent the wheel. Therefore, the best option I can think of is to follow the outline of the regex tutorial built in to Perl, "perlretut", which should be available from any complete Perl install by typing <code>perdoc perlretut</code> on the command line. Perl.org's documentation also has it <a href="https://perldoc.perl.org/perlretut.html" rel="nofollow noreferrer" title="Perl.org online regular expression tutorial">online in HTML</a> for all to use. Granted, all the samples there are given in the context of use within Perl, so they need to be converted for your classroom use. The idea isn't to, necessarily, use their examples, as to follow the sequence they have created. They've invented the wheel, now you get to design the rims and hub caps so that the vehicle you need is "road ready."</p>
  
  <p>To cover the <em>very</em> basic outline, below is the relevant sections from the Table of Contents for <code>perlretut</code>. For the "died-in-the-wool" purists, it even begins with the inexplicably obligatory "Hello World" of program language instruction.</p>
  
  <blockquote>
    <ul>
    <li>Part 1: The basics
    
    <ul>
    <li>Simple word matching</li>
    <li>Using character classes</li>
    <li>Matching this or that</li>
    <li>Grouping things and hierarchical matching</li>
    <li>Extracting matches</li>
    <li>Backreferences</li>
    <li>Relative backreferences</li>
    <li>Named backreferences</li>
    <li>Alternative capture group numbering</li>
    <li>Position information</li>
    <li>Non-capturing groupings</li>
    <li>Matching repetitions</li>
    <li>Possessive quantifiers</li>
    <li>Building a regexp</li>
    <li>Using regular expressions in Perl</li>
    </ul></li>
    <li>Part 2: Power tools
    
    <ul>
    <li>More on characters, strings, and character classes</li>
    <li>Compiling and saving regular expressions</li>
    <li>Composing regular expressions at runtime</li>
    <li>Embedding comments and modifiers in a regular expression</li>
    <li>Looking ahead and looking behind</li>
    <li>Using independent subexpressions to prevent backtracking</li>
    <li>Conditional expressions</li>
    <li>Defining named patterns</li>
    <li>Recursive patterns</li>
    <li>A bit of magic: executing Perl code in a regular expression</li>
    <li>Backtracking control verbs</li>
    <li>Pragmas and debugging</li>
    </ul></li>
    </ul>
  </blockquote>
  
  <p>One additional resource which I have found helpful from time to time is <a href="http://www.regular-expressions.info/" rel="nofollow noreferrer" title="Online regular expression resource">Regular-Expressions.info</a>. They include a reference and a tutorial, among other things. The significance of this site is that it often has a different way of "saying" the same thing as other sites, which can reach a student when the standard explanation just does not "click" for the student.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-10T07:23:32.017Z
Answers.Text: Most importantly: call them regex or regexp, not regular expressions, or the person who later teaches them the Chomsky hierarchy will curse you from the depths of their heart.
Answers.UserDisplayName: Peter Taylor
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-06T17:10:42.240Z
Answers.LastActivityDate: 2017-08-06T17:10:42.240Z
Answers.UserDisplayName: Gypsy Spellweaver
Answers.Body: <p>First and foremost, regular expression is a mathematical concept. Lacking the relevant background and intuition does make it intimidating indeed. Students must have at least a vague understanding of what <code>FA</code> is, and what makes it <code>F</code>. IMO, understanding the abilities and limitations of FAs is the key.</p>
  
  <p>I found that conducting a creation of an regexp interpreter is an eye opener. An ultimate goal of the exercise is to make a <code>grep</code> clone.</p>
  
  <p>Start simple, from <code>strcmp</code>. Introduce a dot. Spend some time explaining what makes it a <em>meta</em> character, and how to tell a meta from a normal character. Introduce a star. Say few words on Kleene theorem, and on how this innocent star is really a centerpiece of a profound result.</p>
  
  <p>At this point students shall have enough intuition to grasp the rest of the syntactic sugar.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-06T00:18:28.283Z
Answers.Text: I dont even know what you mean by `FA` or that being `F`, yet I'm pretty good with regex. Similarly, I don't understand why you think math is needed to use, or understand, them. They're *pattern matching*, right? Not *formula matching*.
Answers.UserDisplayName: Gypsy Spellweaver
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-06T00:24:50.047Z
Answers.Text: @GypsySpellweaver `FA` is finite automaton, where `F` stands for `finite`.
Answers.UserDisplayName: user58697
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-06T00:24:58.320Z
Answers.Text: Ditto what @GypsySpellweaver said.
Answers.UserDisplayName: CS Cheerleader
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-06T00:33:38.170Z
Answers.Text: So, how is knowing any of that important to using regex, in programming or CLI? In implementing a regex interpreter, that might be important, can't say, but that's not part of the question from the OP.
Answers.UserDisplayName: Gypsy Spellweaver
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-06T00:38:07.857Z
Answers.Text: @GypsySpellweaver OP didn't ask of _using_ regex; the question is explicitly about _fundamentals_ and _understanding how it works_. You cannot teach fundamentals without resorting to fundamentals.
Answers.UserDisplayName: user58697
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-06T00:47:25.030Z
Answers.Text: Then one of us misread the question. I interpreted it as an introduction to the use of regex, according to the statement: _The students don't need to know every detail, but do need to know enough to work effectively and learn more_.
Answers.UserDisplayName: Gypsy Spellweaver
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-06T00:53:59.097Z
Answers.Text: @GypsySpellweaver I agree on the misreading. OTOH I have seen enough senior programmers trying to parse stack languages with regexes. I am sure you did too. I do want my students to know what they are doing.
Answers.UserDisplayName: user58697
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-07T02:56:00.070Z
Answers.Text: @nocomprende Glad to hear from a like-minded.
Answers.UserDisplayName: user58697
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-05T22:41:45.493Z
Answers.LastActivityDate: 2017-08-05T22:41:45.493Z
Answers.UserDisplayName: user58697
Body: <p>My students are knowledgeable about Java but need to know something about Regular Expressions. Many students find them difficult and intimidating. The students don't need to know every detail, but <em>do</em> need to know enough to work effectively and learn more. </p>
  
  <p><strong>What are effective ways of teaching the fundamentals of regular expressions in Java or otherwise?</strong> I'd like the students to understand how it works, and not only what each pattern does.</p>
  
  <hr>
  
  <p>This year it's been decided at my school (I'm a teacher assistant this year) to teach the students a bit about regular expressions. We teach in Java.
  The students have never faced anything like regex in the past. The students I've asked (we have a test group - we test new ideas on them for feedback. A diverse group in terms of level and background<sup>1</sup> consisting of 3-4 students) seem to find regex very difficult (we just gave them the pattern list and explain each pattern).</p>
  
  <hr>
  
  <p><sup>1</sup>background in terms of knowledge in the field.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-08-06T18:31:24.447Z
Comments.Text: This is too small for an answer but start by showing them why regular expressions are worth their time to learn.  I found when I was learning Regex, it was a horrid experience until I found use cases where they made my life easy enough to be worth the cost of learning.
Comments.UserDisplayName: Cort Ammon
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-08-06T18:32:27.737Z
Comments.Text: @CortAmmon good point.
Comments.UserDisplayName: ItamarG3
Tags: <lesson-ideas><regex>
Title: Effective ways of teaching regex to students who know Java
ThreadText: # Effective ways of teaching regex to students who know Java
  
  (Asked by: username_0 on 2017-08-05)
  
  My students are knowledgeable about Java but need to know something about Regular Expressions. Many students find them difficult and intimidating. The students don't need to know every detail, but *do* need to know enough to work effectively and learn more.
  
  **What are effective ways of teaching the fundamentals of regular expressions in Java or otherwise?** I'd like the students to understand how it works, and not only what each pattern does.
  
  ---
  
  This year it's been decided at my school (I'm a teacher assistant this year) to teach the students a bit about regular expressions. We teach in Java.
  The students have never faced anything like regex in the past. The students I've asked (we have a test group - we test new ideas on them for feedback. A diverse group in terms of level and background1 consisting of 3-4 students) seem to find regex very difficult (we just gave them the pattern list and explain each pattern).
  
  ---
  
  1background in terms of knowledge in the field.
  
  **Tags:** lesson-ideas, regex
  
  > **Comments:**
  > **username_1**: This is too small for an answer but start by showing them why regular expressions are worth their time to learn.  I found when I was learning Regex, it was a horrid experience until I found use cases where they made my life easy enough to be worth the cost of learning.  
  > **username_0**: @username_1 good point.  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_3 on 2017-08-05. Score: 6
  
  > Many students find them difficult and intimidating.
  
  Then the challenge is to make them fun and engaging and (ideally) interactive. I myself have undertaken the challenge here and there of teaching myself regex, and two resources rose to the top in my own learning: [RegexOne](https://regexone.com/) and [Regex Crossword](https://regexcrossword.com/).
  
  RegexOne is great as a lesson-by-lesson tutorial. Students can type and see in real time what tests cases their regex does/does not match. There are 15.5 lessons and 8 practice problems. The practice problems draw from relevant use cases, such as extracting a phone number, trimming white space, or reading a log file.
  
  Regex Crossword mixes regex with logic: students have to fill in a square grid and track how gradually more complex regular expressions interact with each other. (That sounds a bit vague, but an example like [this one](https://regexcrossword.com/challenges/beginner/puzzles/1) makes it clear what I mean.) The crosswords tend to have fun themes/solutions. There are many crosswords to solve, including ones with great complexity, so students will have to spend lots of time working at it if they'd like to complete all the puzzles.
  
  When it comes to working in class, it'd be pretty easy to work with some of the smaller crosswords in a non-digital manner. You could put one on the board at the start of every class as a warm-up or print/copy some of your favorite from the site. Maybe even have students work in groups and complete to solve a series of puzzles in the fastest possible time.
  
  Between these two resources, students will at least have a pretty good start to working with regex.
  
  > **Comments:**
  > **username_4**: In a similar way you could create a Jeopardy game. The Jeopardy "answers" are REs. The contestants are given an RE for some category and the first to "ring the bell" gets to reply with a valid match - in the form of a question, of course. I used to keep a set of hotel desk bells for this sort of thing. This only works for deciphering/reading, of course. As you proceed through a category the REs get harder. The categories can be RE types or something more elaborate.  
  > **username_5**: A similar activity to RegexOne, without the tutorial text but with larger test suites for each problem, is https://alf.nu/RegexGolf  
  
  ---
  
  ### Answer by username_6 on 2017-08-06. Score: 5
  
  In Java: **Don't**
  
  In programming language *X*: **Don't**
  
  Treat Regular Expression as its own language. Teach regex in a language-agnostic fashion using tools that process regular expressions rather than trying to teach regular expressions **and** their implementation in Java, or any other language. Each language has its own, idiosyncratic, engine and method of use. Different engines implement subsets of the entirety of the regular expression specifications.
  
  As a starting point, use an online version of a regular expression editor. There are two fairly good versions that I'm aware of. First is [Regex101](https://regex101.com/ "Online regular expression editor IDE from Regex101"), which has a decent IDE style interface, including an live explanation of the current expression, and a token list to choose from. The one feature I like is that it allows you to "Save & Share" the expression, which adds it to a library of expressions available to others. Of course, that library is also available to you, so you can try to find one that's already written to do what you need. One major issue I have with this site is that it still does not implement the entire specification of regular expressions. Specifically, it does not handle If-Then-Else conditionals: `(?(subpattern)yes|no)` and `(?(group)yes|no)`.
  
  A second site, which will handle the If-Then-Else conditionals, is [Regex Storm](http://regexstorm.net/tester "Online regular expression editor from Regex Storm"). This one produces a nice set of tables for the "output" of the expression, but lacks the collection of tokens, and library of expressions of Regex101. As near as I have been able to determine, this editor implements the entire set of regular expressions available in all engines extant, including left-to-right text. There is also an extensive reference on this site that can be terse at times, but does include examples for most of the complex, or potentially confusing, constructs.
  
  After selecting the editor to use, local, online, etc., the problem now becomes how to work through the "language" of regex. I don't like to reinvent the wheel. Therefore, the best option I can think of is to follow the outline of the regex tutorial built in to Perl, "perlretut", which should be available from any complete Perl install by typing `perdoc perlretut` on the command line. Perl.org's documentation also has it [online in HTML](https://perldoc.perl.org/perlretut.html "Perl.org online regular expression tutorial") for all to use. Granted, all the samples there are given in the context of use within Perl, so they need to be converted for your classroom use. The idea isn't to, necessarily, use their examples, as to follow the sequence they have created. They've invented the wheel, now you get to design the rims and hub caps so that the vehicle you need is "road ready."
  
  To cover the *very* basic outline, below is the relevant sections from the Table of Contents for `perlretut`. For the "died-in-the-wool" purists, it even begins with the inexplicably obligatory "Hello World" of program language instruction.
  
  > * Part 1: The basics
  >   + Simple word matching
  >   + Using character classes
  >   + Matching this or that
  >   + Grouping things and hierarchical matching
  >   + Extracting matches
  >   + Backreferences
  >   + Relative backreferences
  >   + Named backreferences
  >   + Alternative capture group numbering
  >   + Position information
  >   + Non-capturing groupings
  >   + Matching repetitions
  >   + Possessive quantifiers
  >   + Building a regexp
  >   + Using regular expressions in Perl
  > * Part 2: Power tools
  >   + More on characters, strings, and character classes
  >   + Compiling and saving regular expressions
  >   + Composing regular expressions at runtime
  >   + Embedding comments and modifiers in a regular expression
  >   + Looking ahead and looking behind
  >   + Using independent subexpressions to prevent backtracking
  >   + Conditional expressions
  >   + Defining named patterns
  >   + Recursive patterns
  >   + A bit of magic: executing Perl code in a regular expression
  >   + Backtracking control verbs
  >   + Pragmas and debugging
  
  One additional resource which I have found helpful from time to time is [Regular-Expressions.info](http://www.regular-expressions.info/ "Online regular expression resource"). They include a reference and a tutorial, among other things. The significance of this site is that it often has a different way of "saying" the same thing as other sites, which can reach a student when the standard explanation just does not "click" for the student.
  
  > **Comments:**
  > **username_5**: Most importantly: call them regex or regexp, not regular expressions, or the person who later teaches them the Chomsky hierarchy will curse you from the depths of their heart.  
  
  ---
  
  ### Answer by username_7 on 2017-08-05. Score: 3
  
  First and foremost, regular expression is a mathematical concept. Lacking the relevant background and intuition does make it intimidating indeed. Students must have at least a vague understanding of what `FA` is, and what makes it `F`. IMO, understanding the abilities and limitations of FAs is the key.
  
  I found that conducting a creation of an regexp interpreter is an eye opener. An ultimate goal of the exercise is to make a `grep` clone.
  
  Start simple, from `strcmp`. Introduce a dot. Spend some time explaining what makes it a *meta* character, and how to tell a meta from a normal character. Introduce a star. Say few words on Kleene theorem, and on how this innocent star is really a centerpiece of a profound result.
  
  At this point students shall have enough intuition to grasp the rest of the syntactic sugar.
  
  > **Comments:**
  > **username_6**: I dont even know what you mean by `FA` or that being `F`, yet I'm pretty good with regex. Similarly, I don't understand why you think math is needed to use, or understand, them. They're *pattern matching*, right? Not *formula matching*.  
  > **username_7**: @username_6 `FA` is finite automaton, where `F` stands for `finite`.  
  > **username_8**: Ditto what @username_6 said.  
  > **username_6**: So, how is knowing any of that important to using regex, in programming or CLI? In implementing a regex interpreter, that might be important, can't say, but that's not part of the question from the OP.  
  > **username_7**: @username_6 OP didn't ask of _using_ regex; the question is explicitly about _fundamentals_ and _understanding how it works_. You cannot teach fundamentals without resorting to fundamentals.  
  > **username_6**: Then one of us misread the question. I interpreted it as an introduction to the use of regex, according to the statement: _The students don't need to know every detail, but do need to know enough to work effectively and learn more_.  
  > **username_7**: @username_6 I agree on the misreading. OTOH I have seen enough senior programmers trying to parse stack languages with regexes. I am sure you did too. I do want my students to know what they are doing.  
  > **username_7**: @nocomprende Glad to hear from a like-minded.  
  
  ---
  
  ### Answer by username_2 on 2017-08-06. Score: 1
  
  It is important to know that there are two languages involved, each with its own rules and metacharacters.
  
  Characterclassese is the language of character classes, which are character wildcards. Its special characters include `[ ] ^-`. Every character class stands for a single character. Literal characters are themselves.
  
  Regexese is a second language. Its atoms are character classes. Here are some of its rules
  
  * Juxaposition means "and then immediately."
  * It has an operator for `or` which is `|`
  * Parentheses are delimiters and can override the order of operations
  * There are several postfix multiplicity operators which have higher precedence than juxtaposition.
  * Special character classes include `.^$` These are metacharacters in the
    language as well.
  
  It is important to point out the existence of the two layers and how they work together. "Hedwig," the O'Reilly book on Regexes is great.
  
  
  ---
  
  