C# 新手提问 关于读取TXT 取关键字之间的内容 string[] ay=Regex.Matches(yourstr,"(?<=AAA<[李四).*?(?=]>)").Cast<Match>().Select(m=>m.Value).ToArray(); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我这么写 ay里边是空的啊... 可是txt里 确实应该有要求的东西 void DoTest() { #region 输入 TextBox textbox1 = new TextBox() { Text = "AAA<[" }; TextBox textbox2 = new TextBox() { Text = "]>" }; TextBox textInputBox = new TextBox() { Text = "AAA<[ 张三******************* ]>AAA<[ 李四******************* ]>AAA<[ 王五******************* ]>" }; #endregion #region 将输入转义 string text1 = textbox1.Text, text2 = textbox2.Text; if (String.IsNullOrEmpty(text1) || String.IsNullOrEmpty(text2)) throw new ArgumentException(); Dictionary<string, string> changeDic = new Dictionary<string, string>(); //这里只转义少量的字符 changeDic.Add("<", "\\<"); changeDic.Add(">", "\\>"); changeDic.Add("[", "\\["); changeDic.Add("]", "\\]"); changeDic.ToList().ForEach(x => { text1 = text1.Replace(x.Key, x.Value); text2 = text2.Replace(x.Key, x.Value); }); #endregion string regexText =text1 + @"[\s\w\*]+" + text2; Regex regex = new Regex(regexText); MatchCollection mc = regex.Matches(textInputBox.Text); mc.Cast<Match>().Select(x => { string str = x.Value; str = str.Remove(0, text1.Length - 1); str = str.Remove(str.Length - text2.Length - 1); return str; }).ToArray(); } 你好... 我那个txt文本 字符串不是写死的. 是一较长篇幅。 里边的字符类型 大概是我举例的那样。 正则表达式https://deerchao.net/tutorials/regex/regex.htm 有一个很好的解决办法就是,在txt1 里面 和 txt2 里面 都加上一个特殊字符,保证这个字符的唯一性截取的时候 从这个特殊字符 到 你想要的内容 ,然后过滤这个字符, 比如: textbox1 = AAA<[ textbox1 = textbox1 + "!@#$" textbox1.substring(你想要的字符串) textbox2 = ]> textbox2 = textbox2 + “%&*" textbox2.substring(你想要的字符串) 这是个好办法,给分啊! var str = "<111>AAA< [ 张三******************* ]>AAA< [ 李四******************* ]>AAA< [ 王五******************* ]>"; var all = Regex.Matches(str, @"AAA<[\w\W]*?>"); foreach (Match item in all) { Console.WriteLine(item.Value); } Console.ReadLine(); //用这个吧var str = "AAA<111>AAA< [ 张三******************* ]>AAA< [ 李四******************* ]>AAA< [ 王五******************* ]>"; var all = Regex.Matches(str, @"AAA<\s*\[\s*[\w\W]*?\s*\]\s*>"); foreach (Match item in all) { Console.WriteLine(item.Value); } Console.ReadLine(); 求助 。。 请教一下WINDOWS自动关机的问题 大家好,我问个代码格式化的 c#里如何改变鼠标的光标的形状 超级菜鸟关于数据库连接问题 怎样判断TextBox中输入的是否是日期型字符串呢?谢谢各位了!!! 简单问题,如何去除相同值? 请问在用SQLConnection连接到SQLSERVER时,如何在程序中设置端口 新手上路,请教个TIMER控件的初级问题,不要笑话我哦 在C#下如何使用ActiveX控件 VS2010打开低版本项目~~~请教 求助大神..C#控件接收数据上传数据库数据类型老是不对
我这么写 ay里边是空的啊... 可是txt里 确实应该有要求的东西
void DoTest()
{
#region 输入
TextBox textbox1 = new TextBox() { Text = "AAA<[" };
TextBox textbox2 = new TextBox() { Text = "]>" };
TextBox textInputBox = new TextBox()
{
Text =
"AAA<[ 张三******************* ]>AAA<[ 李四******************* ]>AAA<[ 王五******************* ]>"
};
#endregion #region 将输入转义
string text1 = textbox1.Text, text2 = textbox2.Text;
if (String.IsNullOrEmpty(text1) || String.IsNullOrEmpty(text2))
throw new ArgumentException();
Dictionary<string, string> changeDic = new Dictionary<string, string>();
//这里只转义少量的字符
changeDic.Add("<", "\\<");
changeDic.Add(">", "\\>");
changeDic.Add("[", "\\[");
changeDic.Add("]", "\\]");
changeDic.ToList().ForEach(x =>
{
text1 = text1.Replace(x.Key, x.Value);
text2 = text2.Replace(x.Key, x.Value);
});
#endregion string regexText =text1 + @"[\s\w\*]+" + text2;
Regex regex = new Regex(regexText);
MatchCollection mc = regex.Matches(textInputBox.Text);
mc.Cast<Match>().Select(x =>
{
string str = x.Value;
str = str.Remove(0, text1.Length - 1);
str = str.Remove(str.Length - text2.Length - 1);
return str;
}).ToArray();
}
你好... 我那个txt文本 字符串不是写死的. 是一较长篇幅。 里边的字符类型 大概是我举例的那样。
https://deerchao.net/tutorials/regex/regex.htm
在txt1 里面 和 txt2 里面 都加上一个特殊字符,保证这个字符的唯一性
截取的时候 从这个特殊字符 到 你想要的内容 ,然后过滤这个字符,
比如:
textbox1 = AAA<[ textbox1 = textbox1 + "!@#$" textbox1.substring(你想要的字符串)
textbox2 = ]> textbox2 = textbox2 + “%&*" textbox2.substring(你想要的字符串) 这是个好办法,给分啊!
var all = Regex.Matches(str, @"AAA<[\w\W]*?>");
foreach (Match item in all)
{
Console.WriteLine(item.Value);
} Console.ReadLine();
//用这个吧var str = "AAA<111>AAA< [ 张三******************* ]>AAA< [ 李四******************* ]>AAA< [ 王五******************* ]>";
var all = Regex.Matches(str, @"AAA<\s*\[\s*[\w\W]*?\s*\]\s*>");
foreach (Match item in all)
{
Console.WriteLine(item.Value);
} Console.ReadLine();