过滤字符串 [英] Filtering a string
问题描述
我有一些字符串,我想使用过滤器查看它们是否包含某些字符.
I have some strings and I am wanting to see if they contain certain characters by using a filter.
以下是我正在谈论的那种事情的一个例子......
The following is an example of the kind of thing that I am talking about...
Dim files() As String = IO.Directory.GetFiles("folder path", filter)
除了基于过滤器获取文件之外,我想通过过滤器检查字符串.过滤器可以包含通配符、必需字符等的地方
Except, rather than getting files based on a filter, I am wanting to check a string via a filter. Where the filter can contain wildcards, required characters etc etc
希望这是有道理的...
Hopefully that makes sense...
推荐答案
除了正则表达式之外,VB.NET 还提供了另一种 - 易于使用的方式:
Apart from RegularExpressions, VB.NET provides another - easy-to-use way:
Characters in pattern Matches in string
? Any single character
* Zero or more characters
# Any single digit (0–9)
[ charlist ] Any single character in charlist
[! charlist ] Any single character not in charlist
来自 MSDN 的一些示例
Some examples from MSDN
检查一个七位数的电话号码 phoneNum 是否正好有三个数字,后跟一个空格、一个连字符 (-)、一个句点 (.),或者根本没有字符,后跟四个数字:>
Check for a seven-digit telephone number phoneNum for exactly three numeric digits, followed by a space, a hyphen (–), a period (.), or no character at all, followed by exactly four numeric digits:
Dim sMatch As Boolean =
(phoneNum Like "###[ -.]####") OrElse (phoneNum Like "#######")
其他模式:
Dim testCheck As Boolean
' The following statement returns True (does "F" satisfy "F"?)
testCheck = "F" Like "F"
' The following statement returns False for Option Compare Binary
' and True for Option Compare Text (does "F" satisfy "f"?)
testCheck = "F" Like "f"
' The following statement returns False (does "F" satisfy "FFF"?)
testCheck = "F" Like "FFF"
' The following statement returns True (does "aBBBa" have an "a" at the
' beginning, an "a" at the end, and any number of characters in
' between?)
testCheck = "aBBBa" Like "a*a"
' The following statement returns True (does "F" occur in the set of
' characters from "A" through "Z"?)
testCheck = "F" Like "[A-Z]"
' The following statement returns False (does "F" NOT occur in the
' set of characters from "A" through "Z"?)
testCheck = "F" Like "[!A-Z]"
' The following statement returns True (does "a2a" begin and end with
' an "a" and have any single-digit number in between?)
testCheck = "a2a" Like "a#a"
' The following statement returns True (does "aM5b" begin with an "a",
' followed by any character from the set "L" through "P", followed
' by any single-digit number, and end with any character NOT in
' the character set "c" through "e"?)
testCheck = "aM5b" Like "a[L-P]#[!c-e]"
' The following statement returns True (does "BAT123khg" begin with a
' "B", followed by any single character, followed by a "T", and end
' with zero or more characters of any type?)
testCheck = "BAT123khg" Like "B?T*"
' The following statement returns False (does "CAT123khg"?) begin with
' a "B", followed by any single character, followed by a "T", and
' end with zero or more characters of any type?)
testCheck = "CAT123khg" Like "B?T*"
我很确定引擎盖下的 LIKE-Operator 也是 作为RegularExpression 的一个子集实现,但它更容易用于简单的需求.
I'm pretty sure that the LIKE-Operator under the hood is also implemented as a subset of RegularExpression, but it's easier to use for simple requirements.
以下是 LIKE-Operator 和 RegularExpressions 之间的区别列表:
Here is a list of differences between LIKE-Operator and RegularExpressions:
正则表达式与 Like 运算符 (Visual Basic)一>
我喜欢它;-)
这篇关于过滤字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!