Created
August 21, 2015 10:16
-
-
Save foobartel/15074f851a4ccf026195 to your computer and use it in GitHub Desktop.
Sample file for regex issue with repeat patterns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: Newsletter Design Issues with Outlook 2010 | |
layout: post | |
permalink: /2012/04/newsletter-design-issues-with-outlook-2010/ | |
categories: | |
- Code | |
- Design | |
- Web Dev | |
// The regex should find all categories | |
// categories can range from 1 to unknown (well, sort of…) | |
// output them in the following format: categories: Code, Design, Web Dev | |
// The most inefficient regex used, creating a separate pattern for every category, 7 in this case: | |
categories:\r\s+-\s?((.+)\s?)(\r\s+-\s?(.+)\s?)(\r\s+-\s?(.+)\s?)(\r\s+-\s?(.+)\s?)(\r\s+-\s?(.+)\s?)(\r\s+-\s?(.+)\s?)(\r\s+-\s?(.+)\s?)(\r\s+-\s?(.+)\s?) | |
// replace pattern: | |
categories: \1, \4, \6, \8, \10, \12, \14, \16\r | |
// then change to 6 categories, 5… and repeat. Duh… | |
// What I wanted was somthing like this: | |
categories:\r\s+-\s?((.+)\s?)(\r\s+-\s?(.+)\s?){1,} | |
// but this doesn't work :( | |
// even if this would work, there's no way feed it the commas I guess | |
// thanks for giving it a shot ;) | |
Thanks guys! Will give it a shot. Looks like it's not possible to achieve in one pass, which I was curious about.
Leaving out categories:
and starting to think at the end of the line seems to work quite well also, but still (or only) leaves one , at the end of the line.
\n\s+-\s?(.+)$
and replace with: \1,
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// Get everything starting with "category:" until the next variable
categorytext = cotent.match(/categories:\s(\s+.+\s?)+/)[1];
// Get the single categories
categories = categorytext.match(/(?:\s+-\s(.*))/g);