RegEx在c#中替换字符串中的特定字母 [英] RegEx to replace a specific letter from string in c#

查看:180
本文介绍了RegEx在c#中替换字符串中的特定字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个RegEx来在字符串中用ł替换所有出现的l。只有在以下情况下才能进行更换:



1)l之前有z,







2)应该有一个数字或。在l之后



例如:

1)如果字符串是abc zl12,200,输出应为abczł12, 200

2)如果字符串是zl.00,输出应该是zł.00

3)如果字符串是adbc zlopr,输出应该是adbc zlopr



谢谢!

I need a RegEx to replace all occurrences of "l" by "ł" in string. Replacement should happen only if:

1)there is "z" before "l",

AND

2)there should be a number or "." after "l"

For ex:
1)If string is "abc zl12,200", output should be "abc zł12,200"
2)If string is "zl.00", output should be "zł.00"
3)If string is "adbc zlopr", output should be "adbc zlopr"

Thanks!

推荐答案

将Regex用于这么简单是没有意义的事情。 string.Replace 会做得很好: https://msdn.microsoft.com/en-us/library/czx8s9ts%28v=vs.110%29.aspx [ ^ ]。



对于正则表达式...只是学习它们 - 会很有用。有什么问题? :-)



-SA
It makes no sense to use Regex for such a simple thing. string.Replace will do it just fine: https://msdn.microsoft.com/en-us/library/czx8s9ts%28v=vs.110%29.aspx[^].

As to Regular Expressions… just learn them — will be useful. What's the problem? :-)

—SA


试试这个

string str =abc zl12,200;

str = str.Replace('l','ł');
Try This
string str = "abc zl12,200";
str = str.Replace('l', 'ł');


这篇关于RegEx在c#中替换字符串中的特定字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆