C# 的 Contains() 方法不直接支持正则表达式。如果您想要使用正则表达式来确定字符串是否包含某个模式,可以使用 Regex 类来实现。您可以通过创建一个 Regex 对象,然后使用它的 Match() 方法来检查字符串是否包含与正则表达式匹配的内容。以下是一个简单的示例:
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
string input = "Hello, world!";
string pattern = @"world";
Regex regex = new Regex(pattern);
bool containsPattern = regex.IsMatch(input);
if (containsPattern)
{
Console.WriteLine("Input string contains the pattern.");
}
else
{
Console.WriteLine("Input string does not contain the pattern.");
}
}
}
这段代码演示了如何使用 Regex 类来检查输入字符串是否包含指定的正则表达式模式。您可以根据需要修改正则表达式模式以匹配您想要检查的内容。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:C#如何分析字符串数据