想查找所有的以stra变量开头 以"> 结尾的字符串
C#下 如何写
String Pattern = "\b"+stra+"\S*+@"">"+"\b";
还有regex是否可以查找streamreader对象
C#下 如何写
String Pattern = "\b"+stra+"\S*+@"">"+"\b";
还有regex是否可以查找streamreader对象
调试欢乐多
using System.Collections;
using System.Text.RegularExpressions;public class MyClass
{
public static void Main()
{
String pattern = "";
String stra = "";
String input = "";
RETRY:
Console.WriteLine("请输入要匹配的 stra 变量:");
Console.WriteLine(" -- 如输入 <aaa");
stra = Console.ReadLine();
Console.WriteLine("请输入以要查找的字符串():");
Console.WriteLine(" -- 如输入 <aaa>asfs<aaabbbb>4444");
input = Console.ReadLine();
pattern = "" + stra + "[^>]*>";
Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
MatchCollection mc = r.Matches(input);
if( mc.Count == 0 )
{
Console.WriteLine("无匹配的字符串。");
}
for (int i=0;i<mc.Count;i++)
{
Console.WriteLine("找到字符串 {0}: {1}", (i+1), mc[i].Value);
}
Console.WriteLine("quit/q 退出测试!");
Console.WriteLine();
if(Console.ReadLine().Trim() == "quit" || Console.ReadLine().Trim() == "q")
{
}
else
{
goto RETRY;
}
}
}
String Pattern = "^" + str + ".*\>$";
语法就不可以了
pattern = "^" + stra+ ".*>";语法对了 但找不到结果
我调式了程序 要是对的 绝对有结果
把调式的stra手工在input可以找到11个 符合的字符串就是在调式的时候把input的值copy到文本文件 但是程序找不到
应该是正则表达式不对了
我是用C# 开发 winform
试了
程序如下 调式的时候都有值 input和 stra把调式的stra手工在input可以找到11个 符合的字符串就是在调式的时候把input的值copy到文本文件 但是程序找不到
应该是正则表达式不对了
我是用C# 开发 winform
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;namespace HideApplication
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox tbFile;
private System.Windows.Forms.Button btnBrowse;
private System.Windows.Forms.TextBox tbMsg;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox tbstra;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form2()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.tbFile = new System.Windows.Forms.TextBox();
this.btnBrowse = new System.Windows.Forms.Button();
this.tbMsg = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.tbstra = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// tbFile
//
this.tbFile.Location = new System.Drawing.Point(72, 64);
this.tbFile.Name = "tbFile";
this.tbFile.Size = new System.Drawing.Size(248, 21);
this.tbFile.TabIndex = 0;
this.tbFile.Text = "";
//
// btnBrowse
//
this.btnBrowse.Location = new System.Drawing.Point(336, 64);
this.btnBrowse.Name = "btnBrowse";
this.btnBrowse.Size = new System.Drawing.Size(96, 23);
this.btnBrowse.TabIndex = 1;
this.btnBrowse.Text = "Browse File";
this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click);
//
// tbMsg
//
this.tbMsg.Location = new System.Drawing.Point(16, 96);
this.tbMsg.Multiline = true;
this.tbMsg.Name = "tbMsg";
this.tbMsg.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.tbMsg.Size = new System.Drawing.Size(416, 256);
this.tbMsg.TabIndex = 2;
this.tbMsg.Text = "";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(16, 64);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(54, 17);
this.label1.TabIndex = 3;
this.label1.Text = "文件名:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(16, 16);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(42, 17);
this.label2.TabIndex = 4;
this.label2.Text = "stra:";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// tbstra
//
this.tbstra.Location = new System.Drawing.Point(72, 16);
this.tbstra.Name = "tbstra";
this.tbstra.Size = new System.Drawing.Size(240, 21);
this.tbstra.TabIndex = 5;
this.tbstra.Text = "<a";
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(448, 373);
this.Controls.Add(this.tbstra);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.tbMsg);
this.Controls.Add(this.btnBrowse);
this.Controls.Add(this.tbFile);
this.MaximizeBox = false;
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load);
this.ResumeLayout(false); }
#endregion private void Form2_Load(object sender, System.EventArgs e)
{
} private void btnBrowse_Click(object sender, System.EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "文本文件 (*.txt)|*.txt|所有文件(*.*)|*.*";
openFileDialog.FilterIndex = 1;
openFileDialog.RestoreDirectory = true; if( openFileDialog.ShowDialog() == DialogResult.OK )
{
tbMsg.Text = ""; tbFile.Text = openFileDialog.FileName;
String pattern = "";
String stra = tbstra.Text;
String input = ""; if(!File.Exists(tbFile.Text))
{
MessageBox.Show("请选择要查找的文件!");
return;
} FileStream fs = new FileStream(tbFile.Text, FileMode.Open);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.GetEncoding("gb2312"));
input = sr.ReadToEnd();
pattern = "" + stra + "[^>]*>";
//pattern = "<a[^>]*>";
Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
MatchCollection mc = r.Matches(input);
if( mc.Count == 0 )
{
WriteLine("无匹配的字符串。");
}
for (int i=0;i<mc.Count;i++)
{
WriteLine(String.Format("找到字符串 {0}: {1}", (i+1), mc[i].Value));
}
}
} void WriteLine(string msg)
{
tbMsg.AppendText(msg + "\r\n");
} /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
//Application.Run(new MainForm());
//Application.Run(new Form1());
Application.Run(new Form2());
}
}
}
<a A>aaaa<a B>bbbb<a C>ccccc<b A>ba<c A>ca
<a A>aaaa<a B>bbbb<a C>ccccc<b A>ba<c A>ca
<a A>aaaa<a B>bbbb<a C>ccccc<b A>ba<c A>ca
<a A>aaaa<a B>bbbb<a C>ccccc<b A>ba<c A>ca<a A>aaaa<a B>bbbb<a C>ccccc<b A>ba<c A>ca<a A>aaaa<a B>bbbb<a C>ccccc<b A>ba<c A>ca<a A>aaaa<a B>bbbb<a C>ccccc<b A>ba<c A>ca<a A>aaaa<a B>bbbb<a C>ccccc<b A>ba<c A>ca
可以找的到