当前位置: 代码迷 >> ASP.NET >> 【正则】特殊取值解决思路
  详细解决方案

【正则】特殊取值解决思路

热度:3842   发布时间:2013-02-25 00:00:00.0
【正则】特殊取值
<w:r wsp:rsidRPr="00885B8F"><w:rPr><w:rFonts w:hint="fareast"/><wx:font wx:val="宋体"/><w:sz w:val="30"/><w:sz-cs w:val="30"/></w:rPr><w:t>11111</w:t></w:r>

<w:r wsp:rsidRPr="00885B8F"><w:rPr><w:rFonts w:hint="fareast"/><wx:font wx:val="宋体"/><w:sz w:val="30"/><w:sz-cs w:val="30"/></w:rPr><w:t>222222</w:t></w:r>

<w:r wsp:rsidRPr="00885B8F"><w:rPr><w:rFonts w:hint="fareast"/><wx:font wx:val="宋体"/><w:sz w:val="30"/><w:sz-cs w:val="30"/></w:rPr><w:t>3333</w:t></w:r>

我只想取出带 222的 这段文字
(?is)<w:r\b[^>]*>2\b222\b</w:r>

结果是

<w:r wsp:rsidRPr="00885B8F"><w:rPr><w:rFonts w:hint="fareast"/><wx:font wx:val="宋体"/><w:sz w:val="30"/><w:sz-cs w:val="30"/></w:rPr><w:t>222222</w:t></w:r>

------解决方案--------------------------------------------------------
C# code
            StreamReader reader = new StreamReader("c:\\1.txt");            string source = reader.ReadToEnd();            Regex reg = new Regex(@"<w:r.*<w:t>222222</w:t></w:r>");            Match mm = reg.Match(source);            MessageBox.Show(m.Value);
------解决方案--------------------------------------------------------
C# code
            string str = File.ReadAllText(@"E:\1.txt");            Regex reg = new Regex(@"<w:r\b[^>]*?>.*?<w:t>222222</w:t></w:r>");            foreach (Match m in reg.Matches(str))                Console.WriteLine(m.Value);/*<w:r wsp:rsidRPr="009F3EEF"><w:rPr><w:rFonts w:hint="fareast"/><wx:font wx:val="宋体"/><w:sz w:val="30"/><w:sz-cs w:val="30"/></w:rPr><w:t>222222</w:t></w:r>*/
------解决方案--------------------------------------------------------

code=C#] 
string input="你的字符串";
Regex reg = new Regex(input,@"(?is)(<w:r[^>]*>)[^\1]*?<w:t>222222</w:t></w:r>"); 
Console.WriteLine(m.Value);
[/code]
------解决方案--------------------------------------------------------
C# code
            string inputstr = "你的字符串";            Regex reg = new Regex(@"(?is)(<w:r[^>]*>)[^\1]*?<w:t>222222</w:t></w:r>");            Match mreulststr = reg.Match(inputstr);            Console.WriteLine(mreulststr.Value);
------解决方案--------------------------------------------------------
C# code
            string str = File.ReadAllText(@"E:\1.txt");            Regex reg = new Regex(@"<w:r\b[^>]*?>(?:(?!<w:r\b[^>]*?>).)*<w:t>222222</w:t></w:r>");            foreach (Match m in reg.Matches(str))                Console.WriteLine(m.Value);
  相关解决方案