Regex – How to check if a string is matched the text but not the number?(正则表达式-如何检查字符串是否与文本匹配,而不是与数字匹配?)-c#
Regex – How to check if a string is matched the text but not the number?(正则表达式-如何检查字符串是否与文本匹配,而不是与数字匹配?)
I want to check a string is matched so I will do some business logics with it.
The pattern is very simple.
If it matches 100% text but different number so it’s a match.
Example:
Pattern: “xxx is a large number” which xxx must be a integer number (not null, not empty, not text, not double number)
- “123 is a large number” => match
- “444444 is a large number” => match
- “is a large number” => not match
- “123 is not a large number” => not match
- “Test is a large number” => not match
My code:
var pattern = "^[0-9]+$ is a large number";
var testText = "123 is a large number";
var match = Regex.Match(testText, pattern, RegexOptions.IgnoreCase);
if (match.Success)
{
//do some business logics
}
This is the Regex I try but doesn’t work:
"^[0-9]+$ is a large number"
Thank you.
Solution:
^(\d)+ is a large number$
- ^ for the start of the string
^ for the start of the string
- \d+ for a digit, 1 or more times
\d+ for a digit, 1 or more times
- is a large number$ for the rest of the string (and $ to signify the end)
is a large number$ for the rest of the string (and $ to signify the end)
————————
我想检查一个字符串是否匹配,所以我会用它做一些业务逻辑。
模式非常简单。
如果它与100%文本匹配,但数字不同,那么它是匹配的。
例子:
模式:“xxx是一个大数字”,其中xxx必须是整数(非空、非空、非文本、非双数)
- “123是一个大数字”=>匹配
- “4444是一个大数字”=>匹配
- “是一个大数字”=>不匹配
- “123不是一个大数字”=>不匹配
- “测试是一个大数字”=>不匹配
我的代码:
var pattern = "^[0-9]+$ is a large number";
var testText = "123 is a large number";
var match = Regex.Match(testText, pattern, RegexOptions.IgnoreCase);
if (match.Success)
{
//do some business logics
}
这是我尝试过但不起作用的正则表达式:
"^[0-9]+$ is a large number"
非常感谢。
解决方法:
^(\d)+是一个很大的数字$
- ^开始的时候
^开始的时候
- \d+表示一个数字,1次或多次
\d+表示一个数字,1次或多次
- 字符串的其余部分是一个大数字$(表示结束的是$)
字符串的其余部分是一个大数字$(表示结束的是$)