EonaCat.Network/EonaCat.Network/System/Tools/RegEx.cs

106 lines
3.9 KiB
C#

using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace EonaCat.Network
{
// This file is part of the EonaCat project(s) which is released under the Apache License.
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
public class RegEx
{
/// <summary>
/// Match everything from the source
/// </summary>
/// <returns>matched data.</returns>
/// <param name="sourceData"> source text.</param>
/// <param name="regexPrefix"> matches the prefix.</param>
/// <param name="regexPostFix">match suffix.</param>
public static List<string> FindAll(string sourceData, string regexPrefix, string regexPostFix)
{
return FindAll(sourceData, regexPrefix, regexPostFix, false, true);
}
/// <summary>
/// Match everything from the source.
/// </summary>
/// <returns>matched data.</returns>
/// <param name="sourceData">source text.</param>
/// <param name="regexPattern">Regular expression.</param>
/// <param name="ignoreCase">If set to <c>true</c> Ignore case.</param>
public static List<string> FindAll(string sourceData, string regexPattern, bool ignoreCase)
{
List<string> result = new List<string>();
MatchCollection matches;
if (ignoreCase)
{
matches = Regex.Matches(sourceData, regexPattern, RegexOptions.IgnoreCase);
}
else
{
matches = Regex.Matches(sourceData, regexPattern);
}
foreach (Match matchItem in matches)
{
result.Add(matchItem.Value);
}
return result;
}
/// <summary>
/// Match everything from the source
/// </summary>
/// <returns>matched data.</returns>
/// <param name="sourceData">source text.</param>
/// <param name="regexPrefix">match prefix.</param>
/// <param name="regexPostFix">match suffix.</param>
/// <param name="OnlyDigit">If set to <c>true</c> only extract numbers.</param>
public static List<string> FindAll(string sourceData, string regexPrefix, string regexPostFix, bool OnlyDigit)
{
return FindAll(sourceData, regexPrefix, regexPostFix, OnlyDigit, true);
}
/// <summary>
/// Match everything from the source
/// </summary>
/// <returns>matched data.</returns>
/// <param name="sourceData">source text.</param>
/// <param name="regexPreFix">match prefix.</param>
/// <param name="regexPostFix">match suffix.</param>
/// <param name="OnlyDigit">If set to <c>true</c> only extract numbers.</param>
/// <param name="ignoreCase">If set to <c>true</c> Ignore case.</param>
public static List<string> FindAll(string sourceData, string regexPreFix, string regexPostFix, bool OnlyDigit, bool ignoreCase)
{
List<string> result = new List<string>();
MatchCollection matches;
if (ignoreCase)
{
matches = Regex.Matches(sourceData,
regexPreFix + (OnlyDigit ? @"(\d*?)" : @"(.*?)") + regexPostFix,
RegexOptions.IgnoreCase);
}
else
{
matches = Regex.Matches(sourceData,
regexPreFix + (OnlyDigit ? @"(\d*?)" : @"(.*?)") + regexPostFix);
}
foreach (Match matchItem in matches)
{
result.Add(matchItem.Value
.Replace(regexPreFix, "")
.Replace(regexPostFix, ""));
}
return result;
}
private RegEx()
{ }
}
}