Skip to content

Instantly share code, notes, and snippets.

@foobartel
Created August 21, 2015 10:16
Show Gist options
  • Save foobartel/15074f851a4ccf026195 to your computer and use it in GitHub Desktop.
Save foobartel/15074f851a4ccf026195 to your computer and use it in GitHub Desktop.
Sample file for regex issue with repeat patterns
---
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 ;)
@rodneyrehm
Copy link

var s = `  - Code
  - Design
  - Web Dev`;

s.replace(/\s*-\s*([^\r\n]+)/g, ', $1').slice(2);
s.split(/\s*-\s*/).slice(1).join(', ');
var s = `permalink: /2012/04/newsletter-design-issues-with-outlook-2010/
categories:
  - Code
  - Design
  - Web Dev
  `;

s.match(/categories:\s+-\s+(([^\r\n]+(\s+-\s+[^\r\n]+)*))/)[1].replace(/\s+-\s+/g, ', ')

@fhemberger
Copy link

// 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);

@foobartel
Copy link
Author

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