gpt4 book ai didi

c# - 检测用于创建 RegEx 的文件名模式

转载 作者:太空宇宙 更新时间:2023-11-03 13:17:53 25 4
gpt4 key购买 nike

当使用字符串定义 RegEx 时,我想知道是否有办法让我的代码识别目录中包含的文件中的模式。

目标是使用我们的命名约定重命名这些文件,所以我正在写一些东西来尝试创建要在 RegEx 中使用的表达式。

我已经开始在这里做一些事情,但我认为它不是最好的,而且我不确定如何填写我的 RegEx 表达式的 "{0}" 部分。

    private Regex m_regex;

public string DirPattern(string path, string[] extensions) {
string result = null;
int endPos = 0;
int resLen = 0;
int startLen = 0;
var dir = new DirectoryInfo(path);
foreach (var file in dir.GetFiles()) {
if (extensions.Contains(file.Extension)) {
if (!String.IsNullOrEmpty(result)) {
int sL = 0;
int fileLen = file.Name.Length;
string one = null;
for (int i = 0; i < resLen && i < fileLen; i++) {
if (result[i] == file.Name[i]) {
sL = i + 1;
if (String.IsNullOrEmpty(one)) {
one = file.Name;
} else {
break;
}
}
}
if (!String.IsNullOrEmpty(one)) {
int eP = 0;
int oneLen = one.Length;
for (int i = fileLen - 1; -1 < i; i--) {
if (result[i] == file.Name[i]) {
eP = i - 1;
} else {
break;
}
}
if ((0 < endPos) && (eP == endPos)) {
if ((0 < startLen) && (sL == startLen)) {
result = one.Substring(0, startLen) + "{0}" + one.Substring(endPos);
} else if (0 < sL) {
startLen = sL;
}
} else if (0 < sL) {
startLen = sL;
}
}
} else {
result = file.Name;
resLen = result.Length;
}
}
}
return result;
}

public bool GenerateRexEx(string path, string[] extensions) {
var pattern = DirPattern(path, extensions);
if (!String.IsNullOrEmpty(pattern)) {
m_regex = new Regex(pattern);
return true;
}
return false;
}

这是一个最像我们公司文件的文件列表示例(不允许我发布):

screenshot

更新:

目标是获取名称如下的文件:

FOLDER_PATTERN_1 + MixedContent + FOLDER_PATTERN_2

并使用我们的格式重命名它们:

OUR_PATTERN_1 + MixedContent + OUR_PATTERN_2

这样,我们的软件将能够更有效地搜索文件。

最佳答案

我认为在您的情况下,您只需要查找前缀模式和后缀模式中的字符数。然后你可以简单地用你的模式替换一些字符。我写了一个简单的代码,我测试过它并且可以工作。你可以启发自己并使用我认为相同的方法。无论如何,还有可以改进的地方,但我希望这足以回答您的问题。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
static class Program
{
static void Main()
{
var inputFilenames = new string[]
{
"mtn_flint501-muxed",
"mtn_flint502-muxed",
"mtn_flint503-muxed",
"mtn_flint504-muxed",
"mtn_flint505-muxed",
"mtn_flint506-muxed",
"mtn_flint507-muxed",
"mtn_flint508-muxed",
"mtn_flint509-muxed",
"mtn_flint510-muxed",
"mtn_flint511-muxed",
"mtn_flint512-muxed",
};
var replacedFilenames = ReplaceFileNames(inputFilenames);

for (int i = 0; i < inputFilenames.Length; i++)
{
Console.WriteLine("{0} >> {1}", inputFilenames[i], replacedFilenames[i]);
}
Console.ReadKey();
}

private const string OurPrefixPattern = "Prefix_";
private const string OurPostfixPattern = "_Postfix";

/// <summary>
/// Method which will find the filename's pattern and replace it with our pattern
/// </summary>
/// <param name="fileNames"></param>
/// <returns></returns>
public static string[] ReplaceFileNames(params string[] fileNames)
{
//At first, we will find count of characters, which are same for
//all filenames as prefix and store it to prefixCount variable and
//we will find count of characters which are same for all filenames
//as postfix and store it to postfixCount variable
var prefixCount = int.MaxValue;
var postfixCount = int.MaxValue;
//We will use first filename as the reference one (we will be comparing)
//all filenames with this one
var referenceFilename = fileNames[0];
var reversedReferenceFilename = referenceFilename.ReverseString();
//Lets find the prefixCount and postfixCount
foreach (var filename in fileNames)
{
if (filename == referenceFilename)
{
continue;
}
//Check for prefix count
var firstDifferenceIndex = referenceFilename.GetFirstDifferentIndexWith(filename);
if (firstDifferenceIndex < prefixCount)
{
prefixCount = firstDifferenceIndex;
}
//For postfix count we will do the same, but with reversed strings
firstDifferenceIndex = reversedReferenceFilename.GetFirstDifferentIndexWith(filename.ReverseString());
if (firstDifferenceIndex < postfixCount)
{
postfixCount = firstDifferenceIndex;
}
}
//So now replace given filnames with our prefix and post fix.
//Our regex determines only how many characters should be replaced
var prefixRegexToReplace = string.Format("^.{{{0}}}", prefixCount);
var postfixRegexToReplace = string.Format(".{{{0}}}$", postfixCount);
var result = new string[fileNames.Length];
for (int i = 0; i < fileNames.Length; i++)
{
//Replace the prefix
result[i] = Regex.Replace(fileNames[i], prefixRegexToReplace, OurPrefixPattern);
//Replace the postfix
result[i] = Regex.Replace(result[i], postfixRegexToReplace, OurPostfixPattern);
}
return result;
}

/// <summary>
/// Gets the first index in which the strings has different character
/// </summary>
/// <param name="value"></param>
/// <param name="stringToCompare"></param>
/// <returns></returns>
private static int GetFirstDifferentIndexWith(this string value, string stringToCompare)
{
return value.Zip(stringToCompare, (c1, c2) => c1 == c2).TakeWhile(b => b).Count();
}

/// <summary>
/// Revers given string
/// </summary>
/// <param name="value">String which should be reversed</param>
/// <returns>Reversed string</returns>
private static string ReverseString(this string value)
{
char[] charArray = value.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
}
}

控制台输出如下所示

mtn_flint501-muxed >> Prefix_01_Postfix

mtn_flint502-muxed >> Prefix_02_Postfix

mtn_flint503-muxed >> Prefix_03_Postfix

mtn_flint504-muxed >> Prefix_04_Postfix

mtn_flint505-muxed >> Prefix_05_Postfix

mtn_flint506-muxed >> Prefix_06_Postfix

mtn_flint507-muxed >> Prefix_07_Postfix

mtn_flint508-muxed >> Prefix_08_Postfix

mtn_flint509-muxed >> Prefix_09_Postfix

mtn_flint510-muxed >> Prefix_10_Postfix

mtn_flint511-muxed >> Prefix_11_Postfix

mtn_flint512-muxed >> Prefix_12_Postfix

关于c# - 检测用于创建 RegEx 的文件名模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25453438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com