如何检查组合词(天气字符串,整数或无效输入? [英] How to check combination word (weather string, integer or invalid input ?
问题描述
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace Test222
{
class Program
{
static void Main(string[] args)
{
int n;
Console.WriteLine("Type here...and Check");
string strInput = Console.ReadLine();
int result = 0;
if (int.TryParse(strInput, out result))
{
Console.WriteLine("{0} is number", result);
}
else
{
Console.WriteLine("{0} is not ", strInput);
}
Console.ReadLine();
}
}
}
我尝试过:
i尝试天气字符串或整数,但我需要的是给定输入是整数和字符串的组合
What I have tried:
i tried to weather string or integer but i need when given input is combination of integer and string
推荐答案
看起来你需要学习正则表达式(RegEx)。
Looks like you need to learn Regular Expressions (RegEx).
^[A-Z0*9]+
将匹配字母和数字的混合。
正则表达式类(System.Text.RegularExpressions) [ ^ ]
正则表达式语言 - 快速参考 [< a href =https://msdn.microsoft.com/en-us/library/az24scfc%28v=vs.110%29.aspxtarget = _blanktitle =新窗口> ^ ]
以下是RegEx文档的链接:
perlre - perldoc.perl.org [ ^ ]
以下是帮助构建RegEx并调试它们的工具的链接:
.NET Regex Tester - Regex Storm [ ^ ]
Expresso正则表达式工具 [ ^ ]
这个显示RegEx是一个很好的图表,它真的有助于理解正在做什么RegEx:
Debuggex:在线可视正则表达式测试器。 JavaScript,Python和PCRE。 [ ^ ]
will match a mix of letters and digits.
Regex Class (System.Text.RegularExpressions)[^]
Regular Expression Language - Quick Reference[^]
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
您的问题需要明确。假设您需要进行某种验证,我建议使用正则表达式,您可以在其中定义匹配模式并进行验证。
下面是匹配整数和字符串的示例代码。
Your question need to be clear . Assuming you need to do some sort of validation I would suggest to use regular expression , where you can define the pattern for matching and do a validation .
Below a sample code for matching integers and strings .
// Create the regular expression
string pattern = @"^\w+
这篇关于如何检查组合词(天气字符串,整数或无效输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!