C#正则表达式:移除文本中所有方括号和其中的内容

简介:

有一文本文件,内容如下:

.......

absent ['1bs4nt, 4b'sent]
a.(from)缺席,不在场;漫不经心的

absolute ['1bs4lu:t]
a. 绝对的,完全的

absorb [4b's3:b]
v. 吸收; 吸引,使专心

abstract ['1bstr1kt]
a. 抽象的n. 摘要,提要v. 提(抽)取

.......

方括号中应该是音标,因为不能正常显示,希望将其删除,总共有5000多个单词,用C#正则表达式,代码如下:

 
    
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  System.IO;
None.gif
using  System.Text.RegularExpressions;
None.gif
None.gif
namespace  Test
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//设置字符编码很重要,否则会得到乱码
InBlock.gif
            StreamReader sr = new StreamReader("c:\\1.txt", System.Text.Encoding.Default);
InBlock.gif            StreamWriter sw 
= new StreamWriter("c:\\2.txt",false , System.Text.Encoding.Default);
InBlock.gif            
using(sr)
InBlock.gif            
using(sw)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                String temp
=sr.ReadToEnd();
InBlock.gif                Regex rx 
= new Regex(@"\[.*\]", RegexOptions.Multiline);//多行模式
InBlock.gif
                temp=rx.Replace(temp, "");
InBlock.gif                sw.Write(temp);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif
InBlock.gif            
//Regex Match Group Capture 的用法
InBlock.gif
InBlock.gif            
//string text = "One car red car blue car";
InBlock.gif            
//string pat = @"(?<1>\w+)\s+(?<2>car)\s*";
ExpandedSubBlockStart.gifContractedSubBlock.gif
            /**///// Compile the regular expression.
InBlock.gif            //Regex r = new Regex(pat, RegexOptions.IgnoreCase);
ExpandedSubBlockStart.gifContractedSubBlock.gif
            /**///// Match the regular expression pattern against a text string.
InBlock.gif            //Match m = r.Match(text);
InBlock.gif            
//while (m.Success)
InBlock.gif            
//{
InBlock.gif            
//    // Display the first match and its capture set.
InBlock.gif            
//    System.Console.WriteLine("Match=[" + m + "]");
InBlock.gif            
//    CaptureCollection cc = m.Captures;
InBlock.gif            
//    foreach (Capture c in cc)
InBlock.gif            
//    {
InBlock.gif            
//        System.Console.WriteLine("Capture=[" + c + "]");
InBlock.gif            
//    }
InBlock.gif            
//    // Display Group1 and its capture set.
InBlock.gif            
//    Group g1 = m.Groups[1];
InBlock.gif            
//    System.Console.WriteLine("Group1=[" + g1 + "]");
InBlock.gif            
//    foreach (Capture c1 in g1.Captures)
InBlock.gif            
//    {
InBlock.gif            
//        System.Console.WriteLine("Capture1=[" + c1 + "]");
InBlock.gif            
//    }
InBlock.gif            
//    // Display Group2 and its capture set.
InBlock.gif            
//    Group g2 = m.Groups[2];
InBlock.gif            
//    System.Console.WriteLine("Group2=[" + g2 + "]");
InBlock.gif            
//    foreach (Capture c2 in g2.Captures)
InBlock.gif            
//    {
InBlock.gif            
//        System.Console.WriteLine("Capture2=[" + c2 + "]");
InBlock.gif            
//    }
InBlock.gif            
//    // Advance to the next match.
InBlock.gif            
//    m = m.NextMatch();
InBlock.gif            
//}
InBlock.gif

InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

1、读写文本文件的时候必须要设置字符编码,否则中文会出现乱码;

2、设置多行模式




本文转自左洸博客园博客,原文链接:http://www.cnblogs.com/myqiao/archive/2007/07/29/835176.html,如需转载请自行联系原作者











目录
相关文章
|
4天前
|
C#
C#读取txt文本的行数
C#读取txt文本的行数
57 0
|
6月前
|
开发框架 .NET C#
ASP.Net c# 正则表达式 子表达式 group
ASP.Net c# 正则表达式 子表达式 group
36 0
|
3天前
探索正则表达式:强大文本匹配与处理工具
探索正则表达式:强大文本匹配与处理工具
|
4天前
|
存储 API C#
C# 实现格式化文本导入到Excel
C# 实现格式化文本导入到Excel
|
4天前
|
Rust 监控 安全
【专栏】`ripgrep`(rg)是Linux下快速、内存高效的文本搜索工具,用Rust编写,支持PCRE2正则表达式
【4月更文挑战第28天】`ripgrep`(rg)是Linux下快速、内存高效的文本搜索工具,用Rust编写,支持PCRE2正则表达式。相比`grep`,它在处理大文件和复杂模式时更具优势。安装`rg`可通过软件包管理器,如在Debian系系统中使用`sudo apt install ripgrep`。基本用法包括简单搜索、递归搜索、忽略大小写、显示行号等。高级功能包括固定字符串搜索、多文件匹配、并行搜索、排除选项和区域搜索。适用于日志分析、代码审查等场景,是提升工作效率的利器。
|
4天前
|
存储 SQL 数据库
C# 将 Word 转文本存储到数据库并进行管理
C# 将 Word 转文本存储到数据库并进行管理
|
4天前
|
存储 SQL 数据库
C# 将 Word 转文本存储到数据库并进行管理
C# 将 Word 转文本存储到数据库并进行管理
|
4天前
|
Linux Perl
使用awk和正则表达式过滤文本或字符串 - 详细指南和示例
使用awk和正则表达式过滤文本或字符串 - 详细指南和示例
84 0
|
4天前
|
存储 C# C++
C# 笔记2 - 数组、集合与与文本文件处理
C# 笔记2 - 数组、集合与与文本文件处理
48 0
|
6月前
|
开发框架 自然语言处理 文字识别
一款C#开发的窗口文本提取开源软件
一款C#开发的窗口文本提取开源软件
50 2