Gets the first match in the given string using the given pattern. - CSharp System

CSharp examples for System:String Match

Description

Gets the first match in the given string using the given pattern.

Demo Code


using System.Text.RegularExpressions;
using System.IO;//ww w.  jav  a  2s . co m
using System.Collections.Generic;

public class Main{
        /// <summary>
		/// Gets the first match in the given string using the given pattern.
		/// </summary>
		/// <param name="Input">The string which in the pattern is searched.</param>
		/// <param name="Pattern">The searched pattern. Must contain exactly one capturing group.</param>
		/// <returns>The first capturing group's value of the first match or null if the pattern not found.</returns>
		public static string GetFirstMatch(this string Input, string Pattern) {
			Regex r = new Regex(Pattern);
			Match m = r.Match(Input);

			if (m != null) {
				return m.Groups[1].Value;
			}

			return null;
		}
}

Related Tutorials