首页 新闻 会员 周边

正则表达式问题

0
悬赏园豆:20 [已解决问题] 解决于 2008-06-03 10:26
<DIV class=posthead> <H2><A class=singleposttitle id=AjaxHolder_ctl01_TitleUrl href="http://www.cnblogs.com/laobusiluo/archive/2008/05/28/1209357.html"><FONT color=#0000cc>正则表达式问题</FONT></A> </H2></DIV> <DIV class=postbody> <P>正在学习正则表达式,自己写了一个正则表达式,功能是要把英语和汉语分开,但结果不是我想要的,哪位能否指点一二。程序源代码如下:<BR><FONT face=Verdana>using System;<BR>using System.Text.RegularExpressions;</FONT></P> <P><FONT face=Verdana>public class Test<BR>{</FONT></P> <P><FONT face=Verdana>&nbsp;&nbsp;&nbsp; public static void Main()<BR>&nbsp;&nbsp;&nbsp; {</FONT></P> <P><FONT face=Verdana>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Regex matchRegex = new Regex(@"([\w+\s]+)([\u4e00-\u9fa5]+)");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Match matchMade = matchRegex.Match("accounting principle 会计准则");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GroupCollection matchGroups = matchMade.Groups;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(matchGroups[0].Value);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(matchGroups[1].Value);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(matchGroups[2].Value);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.ReadLine();</FONT></P> <P><FONT face=Verdana>&nbsp;&nbsp;&nbsp; }</FONT></P> <P><FONT face=Verdana>}<BR>结果输出:<BR>accounting principle 会计准则<BR>accounting principle 会计准<BR>则<BR>我想要的结果是:<BR>accounting principle 会计准则<BR>accounting principle<BR>会计准则<BR>请懂正则的指点下,谢谢!</FONT></P></DIV>
问题补充: 我只是想把英文字符串和汉语字符串分开
罗焕军的主页 罗焕军 | 初学一级 | 园豆:180
提问于:2008-06-03 07:59
< >
分享
最佳答案
0
修改正则表达式: ([\w\s]+?)([\u4e00-\u9fa5]+) +?指示正则表达式,使用非贪婪模式;默认正则表达式以贪婪模式执行。
玉开 | 大侠五级 |园豆:8822 | 2008-06-03 09:19
其他回答(1)
0
"accounting 会计 principle 准则",这种情况的时候,楼主想输出什么结果?
致博腾远 | 园豆:1389 (小虾三级) | 2008-06-03 09:12
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册