91 lines
2.0 KiB
C#
91 lines
2.0 KiB
C#
|
|
using System.Collections;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using System.Text.RegularExpressions;
|
||
|
|
using System.Text;
|
||
|
|
|
||
|
|
using System;
|
||
|
|
using System.Linq;
|
||
|
|
using System.IO;
|
||
|
|
using System.Diagnostics;
|
||
|
|
using Godot;
|
||
|
|
|
||
|
|
using Rokojori.Extensions;
|
||
|
|
namespace Rokojori.Extensions;
|
||
|
|
|
||
|
|
public static class StringExtensions
|
||
|
|
{
|
||
|
|
public static bool Matches( this string value, string regex, RegexOptions options = RegexOptions.None )
|
||
|
|
{
|
||
|
|
var match = new Regex( regex, options ).Match( value );
|
||
|
|
return match != null && match.Success;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static List<string> GetMatches( this string value, string regex, RegexOptions options = RegexOptions.None )
|
||
|
|
{
|
||
|
|
var matches = Regex.Matches( value, regex, options);
|
||
|
|
|
||
|
|
if ( matches.Count == 0 )
|
||
|
|
{
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
var list = new List<string>();
|
||
|
|
|
||
|
|
foreach ( var m in matches )
|
||
|
|
{
|
||
|
|
list.Add( ( m as Match ).Value );
|
||
|
|
}
|
||
|
|
|
||
|
|
return list;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static string GetMatch( this string value, string regex, RegexOptions options = RegexOptions.None )
|
||
|
|
{
|
||
|
|
var matches = Regex.Matches( value, regex, options);
|
||
|
|
|
||
|
|
if ( matches.Count == 0 )
|
||
|
|
{
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
foreach ( var m in matches )
|
||
|
|
{
|
||
|
|
return ( m as Match ).Value;
|
||
|
|
}
|
||
|
|
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static string ReplaceRegex( this string value, string regex, string replacement, RegexOptions options = RegexOptions.None )
|
||
|
|
{
|
||
|
|
return value.Replace( new Regex( regex, options ), replacement );
|
||
|
|
}
|
||
|
|
|
||
|
|
public static string IncrementEndNumbersIfPresent( this string value, System.Func<string,bool> containsName )
|
||
|
|
{
|
||
|
|
var endingInts = $"\\d+$";
|
||
|
|
|
||
|
|
while ( containsName( value ) )
|
||
|
|
{
|
||
|
|
|
||
|
|
if ( value.Matches( endingInts ) )
|
||
|
|
{
|
||
|
|
var number = RegexUtility.ParseInt( value.GetMatch( endingInts ) );
|
||
|
|
|
||
|
|
value = value.ReplaceRegex( endingInts, ( number + 1 ) + "" );
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
value += " 2";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return value;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static List<string> SplitLines( this string source )
|
||
|
|
{
|
||
|
|
return Regex.Split( source, "\r\n|\r|\n" ).ToList();
|
||
|
|
}
|
||
|
|
}
|