Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Linkify Add-in for Visual Studio

, 2 Aug 2008 CPOL
Link source code comments to your bug tracker, MSDN, development Wiki and more.
linkifybin_1.2.zip
Linkify.AddIn
Linkify.dll
linkifybin_1pt2.zip
Linkify.AddIn
Linkify.dll
linkifysetup_1.2.zip
LinkifySetup_1.2.exe
linkifysetup_1pt2.zip
LinkifySetup_1.2.exe
linkifysrc_1.2.zip
Linkify
IconDataConvert.exe
Linkify.AddIn
Linkify.csproj.user
linkify.ico
Setup
LinkifySetup.iss
Setup.vcproj.CALIGULA.PH.user
utility
linkifysrc_1pt2.zip
IconDataConvert.exe
Linkify.AddIn
Linkify.csproj.user
linkify.ico
LinkifySetup.iss
Setup.vcproj.CALIGULA.PH.user
Linkify_1pt3_bin.zip
Linkify.dll
Linkify2005.AddIn
Linkify2008.AddIn
Linkify_1pt3_Setup.zip
LinkifySetup.exe
Linkify_1pt3_src.zip
IconDataConvert.exe
imlConfirm.bmp
linkify.ico
Linkify2005.AddIn
Linkify2008.AddIn
LinkifySetup.iss
Setup.vcproj.CALIGULA.PH.user
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)

Share

About the Author

peterchen
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

| Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 2 Aug 2008
Article Copyright 2006 by peterchen
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid