C#通过正则表达式查找匹配的字符串 [英] C# Find a matching string by regex

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

问题描述

我想找出我的字符串是否包含#1,#a,#abc,#123,#abc123dsds之类的文本,等等...(带有一个或多个字符(数字和字母的'#'字符) )。

I want to find out, whether my string contains a text like #1, #a, #abc, #123, #abc123dsds and so on... ('#' character with one or more characters (digits and letters).

到目前为止,我的代码无法正常工作:

My code so far won't work:

string test = "#123";
boolean matches = test.Contains("#.+");

匹配项变量为 false

推荐答案

String.Contains 不接受正则表达式。

String.Contains does not accept a regex.

使用 Regex.IsMatch

var matches = Regex.IsMatch(test, "#.+");

这篇关于C#通过正则表达式查找匹配的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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