如何在Vb.Net中获取模式中的匹配字符串 [英] How Can I Get The Matched String In Pattern In Vb.Net
问题描述
任何人都可以告诉我如何以指定的模式获取匹配的字符串。
例如我使用了这个
Dim myString as String = 嗨!我的名字是XYZ
Regex.IsMatch( 。*我的名字是XYZ,myString)
这里的字符串将匹配。有什么方法可以说什么字符串与通配符匹配...
例如,在上面的情况下嗨!将匹配。*通配符..有没有办法检测它。
提前谢谢
你的参数是错误的方式 - 第一个参数是要测试的字符串,第二个参数是要使用的正则表达式:
正则表达式.IsMatch(myString, 。*我的名字是XYZ)
要检索匹配的详细信息,请使用匹配
方法 [ ^ ],返回 a匹配
对象 [ ^ ]。您还需要将要检索的部分包装在一个组中:(。*)
Dim myString 作为 字符串 = < span class =code-string> 嗨!我的名字是XYZ
Dim 匹配作为匹配= Regex.Match(myString, (。*)我的名字是XYZ)
如果 match.Success 那么
Dim matchedPart As String = match.Groups( 1 )。值
...
结束 如果
正则表达式语言 - 快速参考 [ ^ ]
Can anyone tell me how can I get the matched string in my specified pattern.
For example I used this
Dim myString as String = "Hi! My name is XYZ"
Regex.IsMatch(".* My name is XYZ", myString)
Here the string will be matched. Is there any way which can say what string was matched with wildcard...
For example, In the above case Hi! will be matched with .* wildcard.. Is there any way to detect it.
Thanks in advance
Your parameters are the wrong way round - the first parameter is the string to test, and the second parameter is the regular expression to use:
Regex.IsMatch(myString, ".* My name is XYZ")
To retrieve the details of the match, use theMatch
method[^], which returns aMatch
object[^]. You'll also need to wrap the section you want to retrieve in a group:(.*)
Dim myString As String = "Hi! My name is XYZ" Dim match As Match = Regex.Match(myString, "(.*) My name is XYZ") If match.Success Then Dim matchedPart As String = match.Groups(1).Value ... End If
这篇关于如何在Vb.Net中获取模式中的匹配字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!