diff --git a/README.md b/README.md index 2327a5ae..a44df9a5 100644 --- a/README.md +++ b/README.md @@ -15,3 +15,4 @@ ## Project * [State machines and regexes](projects/state-mach-regex) + diff --git a/objectives/regex/README.md b/objectives/regex/README.md index 365546c7..88b4de06 100644 --- a/objectives/regex/README.md +++ b/objectives/regex/README.md @@ -169,6 +169,18 @@ Write a regex that matches any one or two digit number, followed by a space, followed by the word `monkey`, followed by 0 or 1 `s`s. +#### Answer + +``` js +let regex = /^\d{1,2}\smonkeys?$/ + +regex.test('12 monkeys') // true +regex.test('1 monkey') // true +regex.test('123 monkeys') // false +regex.test('12 monkeyss') // false +regex.test('there are 12 monkeys') // false +``` + ### Convert Regex to State Machine Diagram Draw a state machine diagram for the following regex that matches a clock time. diff --git a/projects/state-mach-regex/extractlinks/extractlinks.js b/projects/state-mach-regex/extractlinks/extractlinks.js index 99d583ec..7665656a 100644 --- a/projects/state-mach-regex/extractlinks/extractlinks.js +++ b/projects/state-mach-regex/extractlinks/extractlinks.js @@ -1,20 +1,27 @@ -const fs = require('fs'); +let fs = require('fs') -const args = process.argv.slice(2); +let readfile = path => fs.readFile(path, 'utf-8', (err, data) => data) + +let args = process.argv.slice(2) if (args.length != 1) { - console.error("usage: extractlinks inputfile"); - process.exit(1); + console.error("usage: extractlinks inputfile") + process.exit(1) } -const filename = args[0]; - -// !!!! IMPLEMENT ME +let filename = args[0] +let regex = /https?:\/\/?[\w.-]+(com|net|org|blog)[\/[\w-]*]*[(?|&)\w+=\w+]*/g -// Read file +let readFile = fs.readFileSync(filename, 'utf8', (err, data) => data) +let urls = readFile.match(regex) -// Set up regex +// Log array of URLs: +console.log(urls) -// Find matches +// Write each URL as a line to urls.txt +let outputFile = 'urls.txt' +let file = fs.createWriteStream(outputFile) +file.on('error', () => {}) +urls.forEach(url => file.write(url + '\n')) +file.end() -// Print all matches diff --git a/projects/state-mach-regex/extractlinks/stackoverflow.html b/projects/state-mach-regex/extractlinks/stackoverflow.html index 64b1b30f..414fbaf5 100644 --- a/projects/state-mach-regex/extractlinks/stackoverflow.html +++ b/projects/state-mach-regex/extractlinks/stackoverflow.html @@ -1,5 +1,7 @@ - + + +
@@ -8,19 +10,23 @@ + - + + - + - + + + @@ -28,94 +34,82 @@ - - - - - - + - - - - -Each month, over 50 million developers come to Stack Overflow to learn, share their knowledge, and build their careers.
+Join the world’s largest developer community.
+ Sign Up + +