Click here to Skip to main content
15,886,078 members
Articles / Programming Languages / C#

Linkify Add-in for Visual Studio

Rate me:
Please Sign up or sign in to vote.
4.59/5 (23 votes)
2 Aug 2008CPOL9 min read 170.8K   433   99  
Link source code comments to your bug tracker, MSDN, development Wiki and more.
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Linkify.utility
{
  // TODO: document that better. WTF is the match/group/capture business?

  public static class RxHelper
  {

    /// <summary>gets the values of all captures as string array</summary>
    /// <param name="captures">the CaptureCollection to transform to a string array. May be null.</param>
    /// <returns><c>null</c> if <c>captures</c> is null or has no items. Otherwise, a string array 
    /// with all capture values in capture order.</returns>
    public static string[] ToStrings(CaptureCollection captures)
    {
      if (captures == null || captures.Count == 0)
        return null;

      string[] result = new string[captures.Count];
      for (int i = 0; i < captures.Count; ++i)
        result[i] = captures[i].Value;
      return result;
    }

    /// <summary>get the specified group</summary>
    /// <remarks>If <c>name</c> is not null, gets the group with the specified name. 
    /// If <c>name</c> is null, gets the first explicit capture if one was specified in the RegEx.
    /// If <c>name</c> is null and no explicit capture was specified in RegEx, gets the entire capture.
    /// The return value is null if the group is not found, ro does not contain any captures.
    /// </remarks>
    /// <param name="m">the RegEx Match to get the group from</param>
    /// <param name="name">Name of the group to get. Can be <c>null</c> (see Remarks)</param>
    /// <returns>The specified group. <c>null</c> if the group does not exist or does not contain any captures.</returns>
    public static Group GetNamedGroup(Match m, string name)
    {
      Group g = null;
      if (name != null)
        g = m.Groups[name];   // named capture
      else if (m.Groups.Count > 1)
        g = m.Groups[1];      // first explicit capture if any is given
      else
        g = m.Groups[0];      // entire capture

      if (g.Captures.Count == 0)
        return null;

      return g;
    }

    /// <summary>returns the specified named or unnamed capture as a string.</summary>
    /// <remarks>For rules for specifying <c>name</c>, see <see cref="GetNamedGroup"/>
    /// </remarks>
    /// <param name="m">the match to get the value from</param>
    /// <param name="name">name of the group to get the value from</param>
    /// <returns>the value of the specified group, or nul if the group does not exist, or does not contain any captures.</returns>
    public static string GetValue(Match m, string name)
    {
      if (m == null)
        return null;

      Group g = GetNamedGroup(m, name);
      return (g != null) ? g.Value : null;
    }

    /// <summary>Gets the value of the first explicit capture, or the entire capture if no explicit capture was specified.</summary>
    /// <param name="m">The match to get the value from</param>
    /// <returns>the value of the first explicit or the entire capture, null if the group does not exist or does nto contsain captures.</returns>
    public static string GetValue(Match m) { return GetValue(m, null); }

    /// <summary>get all captures for the specified group as string array.</summary>
    /// <remarks>For rules for specifying <c>name</c>, see <see cref="GetNamedGroup"/>
    /// <param name="m">The match to get the valeus from.</param>
    /// <param name="name">Name of the group, can be null.</param>
    /// <returns>an array of strings with all values from the specified group. 
    /// null if the group does not exist or contains no capture.</returns>
    public static string[] GetValues(Match m, string name)
    {
      if (m == null)
        return null;
      Group g = GetNamedGroup(m, name);
      return (g != null) ? ToStrings(g.Captures) : null;
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Klippel
Germany Germany
Peter is tired of being called "Mr. Chen", even so certain individuals insist on it. No, he's not chinese.

Peter has seen lots of boxes you youngsters wouldn't even accept as calculators. He is proud of having visited the insides of a 16 Bit Machine.

In his spare time he ponders new ways of turning groceries into biohazards, or tries to coax South American officials to add some stamps to his passport.

Beyond these trivialities Peter works for Klippel[^], a small german company that wants to make mankind happier by selling them novel loudspeaker measurement equipment.


Where are you from?[^]



Please, if you are using one of my articles for anything, just leave me a comment. Seeing that this stuff is actually useful to someone is what keeps me posting and updating them.
Should you happen to not like it, tell me, too

Comments and Discussions