簡體   English   中英

無法在字符串中獲取特定模式

[英]Not able to get specific pattern inside a string

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

我正在開發控制台應用程序。 下面我提到了代碼

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)

我必須對正則表達式進行一些更改。 我嘗試過,但未能成功。 我是regex和c#的新手。 請幫忙 。 提前致謝 ..

您想讓.*變得懶惰(即,盡可能少地匹配字符) .*?
(或者使您的正則表達式類似"i18n\\([^,)]*,[^)]*\\)" )。

如果您想要多個匹配項,那么您可能應該有一個while循環。

這個:

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(.*?),(.*?)\)";
Match match = Regex.Match(item, reg1, RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase);
while (match.Success)
{
    string strVal = match.Groups[0].Value;
    Console.WriteLine(strVal);
    match = match.NextMatch();
}

打印:

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

現場演示

您可以嘗試以下正則表達式:

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

它的意思是:匹配i18n並捕獲以open(,后跟除close之外的任何字符)然后具有close)的組

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM