关于C#正则表达式的问题? [英] Question about C# regex?

查看:104
本文介绍了关于C#正则表达式的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我需要制作一个模式,这是我的第一个程序,我使用这个正则表达式的东西,我不太熟悉它,基本上我需要让字符串有更好的空间,但如果我把它放在模式中,对于正则表达式,这意味着,它必须有一个空格,所以如何制作,正则表达式不关心是否有空格?

和我也需要允许/和\,



所以如何在正则表达式的字符串中允许,/和\基本上跳过它们?

i希望你理解我。有关更多信息,请问我。 tnx提前。



btw \这意味着必须有空格:/这对我没用。

解决方案

取决于你想要做什么 - 从你的描述中不清楚。

为了匹配你的空白收藏很容易:

< pre lang =text> [\\\\\ /]

将匹配任何一个角色,并且

 [^ \\\\\ /] 

将匹配任何不属于它们的字符。



但是没有代码说忽略这些字符,无论它们在字符串中的哪个位置 - 对于那种情况,你最好在将数据传递给正则表达式进行匹配之前,使用C#中的string.Replace删除它们。



如果你打算使用正则表达式,那么得到一份 Expresso [ ^ ] - 它是免费的,它会检查并生成正则表达式。我经常使用它!


试试这个:

  string  input =  这是一个带空格的输入字符串。; 
if (Regex.IsMatch(输入, @ [/ \\]))
{
// 字符串包含空格,斜杠或反斜杠或它们的混合
}
else
{
// 字符串不包含任何字符
}


其他人已经回答了您的具体问题,但我想指出一些可以帮助您学习RegEx的网站。

A非常有用的网站是 regexlib [ ^ ];他们有一个庞大的正则表达式库。他们还有一个 RegEx测试人员 [ ^ ],这在尝试查看您的reg ex实际执行的操作时非常有用。此外,还有一个非常有用的RegEx工具此处 [ ^ ]。


Hi guys, i need to make a pattern, this is my first program where i use this regex thing and I'am not much familiar with it, basically i need to allow the string to have space ot better " ", but if i put that in the pattern, for the regex, that means, it must have a whitespace, so how to make, for regex to dont care if there is a whitespace or not?
and i also need to allow "/" and "\",

so how to allow " ", "/" and "\" in the string for the regex basically to skip them?
i hope you understand me. for more info ask me. tnx in advance.

btw \s means must have whitespace :/ which is not useful for me.

解决方案

Depends on what you are trying to do - and it isn't clear from your description.
To match your "whitespace" collection is easy:

[\s\\/]

will match any character which is any one of them, and

[^\s\\/]

will match any character which is none of them.

But there isn't a code which says "ignore these characters, regardless of where they are in the string" - for that kind of thing, you are better off using string.Replace in C# to remove them before passing the data to a Regex for matching.

And if you are going to work with regexes, then get a copy of Expresso [^] - it's free, and it examines and generates Regular expressions. I use it a lot!


Try this:

string input = "This is an input string with spaces.";
if (Regex.IsMatch(input, @"[ /\\]"))
{
    // string contains a space, a slash, or a backslash or a mix of them
}
else
{
    // string does not contain any of those chars
}


The others have already answered your specific question, but I wanted to point out a couple sites that will help you to learn RegEx.
A very helpful site is regexlib[^]; they have a large library of Regular Expressions. They also have a RegEx tester[^], which is very useful when trying to see what your reg ex actually does. Also a very useful RegEx tool is available here[^].


这篇关于关于C#正则表达式的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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