如何对字符串进行正则表达式 [英] how to regular expression for a string

查看:91
本文介绍了如何对字符串进行正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有包含诸如
之类的文本的数组字符串
abcdefghi

a.bec0123

hijk.2.3lmop

strin458


现在我该如何写一个正则表达式,如果文本不包含任何特殊字符,那么我必须将文本替换为",例如:"strin458"

在此先感谢

Hi
i have string of arrays that contains texts like

abcdefghi

a.bec0123

hijk.2.3lmop

strin458


now how can i write a regular expression that if the text doesnot contains any special characters then i have to replace the text with the "" eg:"strin458"

Thanks in advance

推荐答案

我不一定要使用正则表达式来完全做到这一点:
I wouldn''t necessarily try to do this completely with a regex:
string myString = "string56";
Regex r = new Regex(@"[\.\:\""\'\\]");
if (!r.IsMatch( myString ))
    {
    myString = string.Format( @"""{0}""", myString );
    }
Console.WriteLine( myString );


看看这个出色的RegExp在线编辑器和测试器:
http://gskinner.com/RegExr/ [ ^ ]

它是一个很棒的工具,尽管确实需要一些时间来适应它,但是最近它基本上已经教会了我RegExp.尝试尝试不同的事物!侧栏包含所有RegExp符号,转义字符,表达式等,以及产生真正复杂的RegExp所需的示例.它甚至可以为您突出显示匹配项,并让您查找并替换测试.它还会在您键入时进行测试" :)最后一件事是,它将拆分表达式以供您显示不同的组和任何错误.

希望这会有所帮助,
埃德:)
Have a look at this great RegExp online editor and tester:
http://gskinner.com/RegExr/[^]

It''s a fabulous tool though it does take a bit of getting used to it has recently basically taught me RegExp. Just try playing around with different things! The side bar contains all the RegExp symbols, escaped characters, expressions etc and examples that you need to produce really complex RegExp. It even highlights matches for you and will let you do find and replace tests to. It also ''tests as you type'' :) Final thing is that it will split your expression up for you to show the different groups and any errors.

Hope this helps,
Ed :)


这篇关于如何对字符串进行正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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