这个 C# 正则表达式怎么写呢?

Jave.Lin Jave.Lin 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我想获取_T("内部所有内容")中的“内部所有内容”
_T("_T()")这种情况如果也要去获取,应该怎么写。。。想这种结果应该是:"_T()"
这正则应该怎么写呢。。。

我的DEMO还有问题:
WPF的:
xaml:

<Window x:Class="RegexpTestingProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button x:Name="btn1" Content="Button" HorizontalAlignment="Left" Margin="200,219,0,0" VerticalAlignment="Top" Width="75"/>
<TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="156" Margin="40,34,0,0" TextWrapping="Wrap" Text="TextBox_T('test1')('test2')" VerticalAlignment="Top" Width="417"/>

</Grid>
</Window>



.cs:

using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Documents;

namespace RegexpTestingProject
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private const string p1 = "_T\\(\"(.*)\"\\)";
private const string p2 = "(_T\\('([^\\('\\)]+)'\\))|(_T\\(\"([^\\(\"\\)]+)\"\\))";

private Regex r1;
private Regex r2;

public List<Regex> rs;

public MainWindow()
{
InitializeComponent();
this.textBox1.Text = "_T(\"这是带有\"号的字符串内容,不可以配置,但我也想配置\")_T('这是带有'号字符串内容,不可以配置,但我也想配置')";
this.textBox1.Text += "_T(\"这是没有带双引号,并使用双引号引起来的字符串内容,可以匹配\")_T('这是没有带单引号,并使用单引号引起来的字符串内容,可以匹配')";
this.textBox1.Text += "_T(\"test3\")\n(\"tes\n_T('test5')\nt\n_T(\"test6\")\n4\")";
this.btn1.Click += btn1_Click;

r1 = new Regex(p1);
r2 = new Regex(p2, RegexOptions.Multiline);

rs = new List<Regex>(new Regex[] {
//r1,
r2
});
}

void btn1_Click(object sender, RoutedEventArgs e)
{
string txt = this.textBox1.Text;

List<string> result = pickUpAllLanguageStr(txt);
string reusltStr = string.Empty;
foreach (var item in result)
{
reusltStr += "\t" + item + "\n";
}

MessageBox.Show("\npick up result : " + reusltStr);
}

public List<string> pickUpAllLanguageStr(string inputTxt)
{
List<string> result = new List<string>();
foreach (var r in rs)
{
MatchCollection mc = r.Matches(inputTxt);
for (int i = 0; i < mc.Count; i++)
{
var g1 = mc[i].Groups[2].Value;
if (string.IsNullOrEmpty(g1))
{
g1 = mc[i].Groups[4].Value;
}
if (string.IsNullOrEmpty(g1))
{
continue;
}
if (!result.Contains(g1))
{
result.Add(g1);
}
}
}
return result;
}
}
}



运行结果,截图:
...全文
评论 7
收藏 已收藏
分享
返回顶部
Jave.Lin
C# 111,129 成员 642,540 内容