首页 新闻 会员 周边

正则表达式格式化字符串

0
悬赏园豆:100 [已解决问题] 解决于 2008-07-24 09:46
<P>&nbsp;名字格式化对齐</P> <P>&nbsp;王小强</P> <P>&nbsp;张三</P> <P>&nbsp;董强</P> <P>========</P> <P>要求格式化效果 </P> <P>王小强</P> <P>张&nbsp; 三</P> <P>董&nbsp; 强</P> <P>请问我格式化成这样的正则应该怎么写!</P> <P>&nbsp;</P>
问题补充: 我现在是临时那加了空格 判断长度如果是2个我就在中间加个空格的 字符串变化多 感觉还是用正则来的灵活!
冰之印记的主页 冰之印记 | 初学一级 | 园豆:10
提问于:2008-07-23 14:03
< >
分享
最佳答案
0
string names = @" 王小强 张三 董强"; //使用了正则表达式的"零宽断言",可以参考这里:  //http://unibetter.com/deerchao/zhengzhe-biaodashi-jiaocheng-se.htm#lookaround Regex re = new Regex(@"(?<=^\w)(?=\w$)", RegexOptions.Multiline); string newNames = re.Replace(names, " "); Console.WriteLine(newNames) //results: //王小强 //张 三 //董 强
deerchao | 大侠五级 |园豆:8367 | 2008-07-23 22:49
其他回答(1)
0
string regex = "^[\u4e00-\u9fa5]{2}$"; string source="姓名"; if (Regex.Match(source, regex).Success) { Console.WriteLine(source.Insert(1, " ")); } else { Console.WriteLine(source); }
侯垒 | 园豆:3435 (老鸟四级) | 2008-07-23 15:36
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册