C# 无法在字符串中获取特定模式

C# 无法在字符串中获取特定模式,c#,regex,C#,Regex,我想在一个字符串中找到一个特定的子字符串模式。在某种程度上,我可以得到但不完全是我想要提取的 我正在开发一个控制台应用程序。下面我提到了代码 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Text.RegularExpressions; namespac

我想在一个字符串中找到一个特定的子字符串模式。在某种程度上,我可以得到但不完全是我想要提取的

我正在开发一个控制台应用程序。下面我提到了代码

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string item = @"wewe=23213123i18n("""", test. ),cstr(12),i18n("""",test3)hdsghwgdhwsgd)"; 
            item = @"MsgBox(I18N(CStr(539)," + "Cannot migrate to the same panel type.)" +", MsgBoxStyle.Exclamation, DOWNLOAD_CAPTION)";
            string reg1 = @"i18n(.*),(.*)\)";
            string strVal = Regex.Match(item, reg1, RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase).Groups[0].Value;
            List<string> str = new List<string> ();
            str.Add(strVal);
            System.IO.File.WriteAllLines(@"C:\Users\E543925.PACRIM1\Desktop\Tools\Test.txt", str);
        }
    }
}


Expected output -  I18N(CStr(539)," + "Cannot migrate to the same panel type.)
Actual output -  I18N(CStr(539),Cannot migrate to the samepaneltype.),MsgBoxStyle.Exclamation, DOWNLOAD_CAPTION)
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Text.RegularExpressions;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串项=@“wewe=23213123i18n(“”,test.),cstr(12),i18n(“”,test3)hdsghwgdhwsgd)”;
item=@“MsgBox(I18N(CStr(539),“+”无法迁移到相同的面板类型。)“+”,MsgBoxStyle.感叹号,下载标题)”;
字符串reg1=@“i18n(.*),(.*)\”;
字符串strVal=Regex.Match(item,reg1,RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase)。组[0]。值;
List str=新列表();
str.Add(strVal);
System.IO.File.writeAllines(@“C:\Users\E543925.PACRIM1\Desktop\Tools\Test.txt”,str);
}
}
}
预期输出-I18N(CStr(539),“+”无法迁移到同一面板类型。)
实际输出-I18N(CStr(539),无法迁移到samepaneltype.),MsgBoxStyle.感叹号,下载标题)
我必须对正则表达式做一些修改。我试过了,但没有成功。 我不熟悉正则表达式和c。 请帮忙。
提前感谢。

您可以尝试此正则表达式:

i18n(\([^\)]*\))

这意味着:匹配以open(,后跟除closed之外的任何字符)开头的i18n和capture组,然后使用closed)

您可以尝试以下正则表达式:

i18n(\([^\)]*\))

这意味着:匹配i18n和捕获组,这些组以打开(,后跟除关闭字符以外的任何字符)开头,然后有一个关闭的)

您想让
*
变懒(即尽可能少地匹配字符)与
*?

(或者将您的正则表达式改为类似于
“i18n\([^,)]*,[^)]*\”

如果需要多个匹配项,那么可能需要一个while循环

这:

印刷品:

I18N(CStr(539),Cannot migrate to the same panel type.)

.

您希望使
*
*?

延迟(即尽可能少地匹配字符) (或者将您的正则表达式改为类似于
“i18n\([^,)]*,[^)]*\”

如果需要多个匹配项,那么可能需要一个while循环

这:

印刷品:

I18N(CStr(539),Cannot migrate to the same panel type.)

。你能核实一下实际结果和预期结果吗谢谢你标记这一点。。编辑了问题。我得到
I18N(CStr(539),无法迁移到相同的面板类型。),MsgBoxStyle。感叹号,下载标题)
作为输出。你能核实一下实际结果和预期结果吗谢谢你标记这一点。。编辑了这个问题。