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、设置多行模式









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值