关于C#正则表达式的问题?

编程入门 行业动态 更新时间:2024-10-10 09:25:45
本文介绍了关于C#正则表达式的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好,我需要制作一个模式,这是我的第一个程序,我使用这个正则表达式的东西,我不太熟悉它,基本上我需要让字符串有更好的空间,但如果我把它放在模式中,对于正则表达式,这意味着,它必须有一个空格,所以如何制作,正则表达式不关心是否有空格? 和我也需要允许/和\, 所以如何在正则表达式的字符串中允许,/和\基本上跳过它们? i希望你理解我。有关更多信息,请问我。 tnx提前。 btw \这意味着必须有空格:/这对我没用。

解决方案

取决于你想要做什么 - 从你的描述中不清楚。 为了匹配你的空白收藏很容易: [\\\\\ /]

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

[^ \\\\\ /]

将匹配任何不属于它们的字符。 但是没有代码说忽略这些字符,无论它们在字符串中的哪个位置 - 对于那种情况,你最好在将数据传递给正则表达式进行匹配之前,使用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#正则表达式的问题?

本文发布于:2023-11-06 03:38:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562624.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正则表达式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!