Files
2026-06-01 12:28:44 +09:00

62 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace KssSmaPlaLib.Commons
{
public class StringUxer
{
public static bool WildcardMatch(string input, string pattern)
{
// "*" を ".*" に変換し、正規表現にする
string regexPattern = "^" + Regex.Escape(pattern).Replace("\\*", ".*") + "$";
return Regex.IsMatch(input, regexPattern, RegexOptions.IgnoreCase);
}
// public static bool TryParseIniSignature(string iniValue, out string functionName, out List<string> args)
// {
// functionName = null;
// args = null;
// var pattern = @"^([a-zA-Z_][a-zA-Z0-9_]*)\((.*?)\)$";
// var match = Regex.Match(iniValue.Trim(), pattern);
// if (!match.Success)
// return false;
// functionName = match.Groups[1].Value;
// var argsString = match.Groups[2].Value;
// args = string.IsNullOrEmpty(argsString)
// ? new List<string>()
// : argsString.Split(',').Select(arg => arg.Trim()).ToList();
// return true;
// }
public static bool TryExtractYearFromString(string str, out int year)
{
// var match = Regex.Match(str, @"\b(20\d{2})\b"); // 条件厳しめ
var match = Regex.Match(str, @"(20\d{2})"); // 条件緩め
year = match.Success ? int.Parse(match.Groups[1].Value) : -1;
return year != -1;
}
/// <summary>
/// CSVから文字列取得(インデックス指定:範囲外の時既定値)
/// </summary>
/// <param name="csv">CSV文字列(カンマ区切り)</param>
/// <param name="index">インデックス</param>
/// <param name="noString">範囲外の時の既定値</param>
/// <returns>取得した文字列</returns>
public static string GetStringByIndexFormCsv(string csv, int index, string noString = "(No name...)")
{
string[] strSplit = csv.Split(',');
if (index < 0 || index > strSplit.Length - 1) return noString;
return strSplit[index];
}
}
}