Skip to content

Instantly share code, notes, and snippets.

@kobi
Created January 4, 2011 21:11
Show Gist options
  • Save kobi/765417 to your computer and use it in GitHub Desktop.
Save kobi/765417 to your computer and use it in GitHub Desktop.
.Net Regular Expressions - Finding Acronyms, and Reversing the Stack - Sample Program
using System;
using System.Text;
using System.Text.RegularExpressions;
// code for http://kobikobi.wordpress.com/2011/01/04/net-regular-expressions-finding-acronyms-and-reversing-the-stack/
public class Test
{
public static void Main()
{
string findAcronyms = @"
\b((?<Acronym>\w)\w*\W+)+
(?<=(?<-Acronym>.(?=.*?(?<Reverse>\k<Acronym>)))+)(?(Acronym)(?!))
\((?<-Reverse>\k<Reverse>)+\)
(?(Reverse)(?!))";
string input = @"Hello World (hw) and maybe Home work (HW),
And this: Are We Going To Have To Go Through ALL This Again? (AWGTHTGTATA),
but not this (tn), a b c (Abc) d e f (ef) g (g) , yooo (o),
From beneath you, it devours (fbyid)";
MatchCollection matches = Regex.Matches(input, findAcronyms,
RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);
foreach (Match match in matches)
{
Console.WriteLine(match.Value);
}
Console.ReadKey();
}
}
Hello World (hw)
Home work (HW)
Are We Going To Have To Go Through ALL This Again? (AWGTHTGTATA)
a b c (Abc)
e f (ef)
g (g)
From beneath you, it devours (fbyid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment