Skip to content

Instantly share code, notes, and snippets.

@staafl
Last active August 29, 2015 14:10
Show Gist options
  • Save staafl/685f8855a059e78b8044 to your computer and use it in GitHub Desktop.
Save staafl/685f8855a059e78b8044 to your computer and use it in GitHub Desktop.
extract-pattern-from-file.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
class Program
{
static int Main(string[] args)
{
foreach (string line in args[0] == "-" ? ReadConsole() : File.ReadLines(args[0]))
{
Match mm = Regex.Match(line, args[1]);
if (mm.Success)
{
Console.WriteLine(
mm.Groups.Count == 1
? mm.Value
: string.Join("", mm.Groups.OfType<Group>().Skip(1).Select(g => g.Value)));
return 0;
}
}
return 1;
}
static IEnumerable<string> ReadConsole()
{
while (true)
{
string line = Console.ReadLine();
if (line == null)
{
yield break;
}
else
{
yield return line;
}
}
}
}
for /F %x in ('extract-pattern-from-file.exe dot.core.nuspec "<version>([^<]+)"') do set VERSION=%x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment