13 October 2020

A Very Modest Introduction to Streams

What is a Stream? It's Netflix for collections.

image of river styx

forEach

filter

map

Multiplicity Operators

This table shows the multiplicity operators.

* zero or more of
+ one or more of
? one or none
{n} exactly n of
{n,} at least n of
{m, n} at least m but not more than n of

Orring

inmage of regex101.com

Some Practice Challenges

  1. Write a regex that matches a valid decimal integer. Here is a test string. You want to match the first three and not match the second two.
    s = "-243 89 +45 4a3 5.6"
  2. Write a regex that matches any alternating sequence of the letters a and b. Here is a test string. The last three should not match.
    s = "ababab a bababa b bab aba baa aab abc"
  3. Write a regex that matches any alternating sequence of the letters a, b, and c. Here is a test string. The last four should not match.
    s = "cabcab c a  b bcabcabc ca bc  cba aac cca abcd"
  4. Write a regex that matches a 6-digit RGB color code. Be case-insensitive (use a flag).
    s = "0xFFFFCC #FFF8E7 0xaabbcc #aaBBCC #FFFFF #00FF0 0xabc"
  5. Write a regex that matches any word containing the vowels a, e, i, o, u in order within the word. Match the first two but not the second two, case insensitive.
     s = "facetiously autoeciously unseriously aeration"
  6. 
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class Challenge
    {
        public static void printParseableIntegers(String s)
        {
            Pattern p = Pattern.compile("\\b[-+]?\\d+\\b");
            Matcher m = p.matcher(s);
            m.results().forEach(e -> System.out.println(e.group()));
        }
        public static void main(String[] args)
        {
            //how to stop the period?
            printParseableIntegers("-45 + 56 889 4a5 5.6");
            printABAlternating("ababab a bababa b bab aba baa aab abc");
            printValidHexCodes("0xFFFFCC #FFF8E7 0xaabbcc #aaBBCC #FFFFF #00FF0 0xabc");
            printFiveInOrder("facetiously autoeciously unseriously aeration");
            printAlreadyBeenChewed("cabcab c a  b bcabcabc ca bc  cba aac cca abcd");
        }
        public static void printABAlternating(String s)
        {
            Pattern p = Pattern.compile("\\bb?(ab)*a?\\b");
            Matcher m = p.matcher(s);
            m.results().forEach(e -> System.out.println(e.group()));
        }
        public static void printValidHexCodes(String s)
        {
            Pattern p = Pattern.compile("(0x|[#])[0-9a-fA-F]{6}\\b");
            Matcher m = p.matcher(s);
            m.results().forEach(e -> System.out.println(e.group()));
        }
        public static void printFiveInOrder(String s)
        {
            Pattern p = Pattern.compile("\\w*a\\w*e\\w*i\\w*o\\w*u\\w*");
            Matcher m = p.matcher(s);
            m.results().forEach(e -> System.out.println(e.group()));
        }
        public static void printAlreadyBeenChewed(String s)
        {
            Pattern p = Pattern.compile("\\b(c|bc)?(abc)*(a|ab)?\\b");
            Matcher m = p.matcher(s);
            m.results().forEach(e -> System.out.println(e.group()));
        }
    }
    
    

    Grip.java This program will take two command-line arguments. The first is a file name, and the second is a regex. This program will cause all lines containing a match to the regex to be put to stdout.

    Extractor.java This program will take two command-line arguments. The first is a file name, and the second is a regex. This program will locate all matches for the regex in the file and place them in an output file.

    
    public class Extractor
    {
        String fileName;
        String regex;
        public static void main(String[] args)
        {
        }
    }