PowerApps 中的 IsMatch 函数IsMatch function in PowerApps

测试文本字符串是否符合某种模式。Tests whether a text string matches a pattern.

说明Description

IsMatch 函数用于测试文本字符串是否与包含普通字符、预定义模式或正则表达式的某种模式相符。The IsMatch function tests whether a text string matches a pattern that can comprise ordinary characters, predefined patterns, or a regular expression.

使用 IsMatch 函数可验证用户在文本输入控件中输入的内容。Use IsMatch to validate what a user has typed in a Text input control. 例如,可以在将结果保存到数据源中之前验证用户输入的电子邮件地址是否有效。For example, you can confirm whether the user has entered a valid email address before the result is saved to your data source. 如果输入的内容与条件不符,可添加其他控件提示用户更正输入。If the entry doesn't match your criteria, add other controls that prompt the user to correct the entry.

默认情况下,IsMatch 函数在匹配整个文本字符串时区分大小写。By default, IsMatch performs a case-sensitive match for the entire text string. 你可以通过指定一个或多个 MatchOptions 来修改这种行为。You can modify this behavior by specifying one or more MatchOptions.

如果文本字符串与模式相符,则 IsMatch 函数返回 true;否则,返回 falseIsMatch returns true if the text string matches the pattern or false if it doesn't.

模式Patterns

使用 IsMatch 函数的关键在于描述要匹配的模式。The key to using IsMatch is in describing the pattern to match. 你可以组合使用以下文本字符串来描述模式:You describe the pattern in a text string as a combination of:

  • 普通字符,如 "abc""123"Ordinary characters, such as "abc" or "123".
  • 预定义模式,如 LetterMultipleDigitsEmailPredefined patterns, such as Letter, MultipleDigits, or Email. Match 枚举定义了这些模式。)(The Match enum defines these patterns.)
  • 正则表达式代码,如 "\d+\s+\d+""[a-z]+"Regular expressions codes, such as "\d+\s+\d+" or "[a-z]+".

通过使用字符串连接运算符 & 可以组合这些元素。Combine these elements by using the string concatenation operator &. 例如,"abc" & Digit & "\s+" 是一个有效的模式,它表示首先要与字符“a”、“b”和“c”匹配,后面跟一个从 0 到 9 的数字,后面再跟至少一个空格字符。For example, "abc" & Digit & "\s+" is a valid pattern that matches the characters "a", "b", and "c", followed by a digit from 0 to 9, followed by at least one whitespace character.

普通字符Ordinary characters

最简单的模式是一系列要完全匹配的普通字符。The simplest pattern is a sequence of ordinary characters to be matched exactly.

例如,字符串“Hello”与模式 "Hello" 完全匹配。For example, the string "Hello" matches the pattern "Hello" exactly. 不多不少,正好相符。No more and no less. 字符串“hello!”The string "hello!" 就与这个模式不匹配,因为它的末尾有一个感叹号,并且字母“h”的大小写错误。doesn't match the pattern because of the exclamation point on the end and the case is wrong for the letter "h". (有关修改这种行为的方法,请参阅 MatchOptions。)(See MatchOptions for ways to modify this behavior.)

模式语言中有一些保留字符,这些字符有特殊的用途。In the pattern language, certain characters are reserved for special purposes. 要使用这些字符,请使用 \(反斜杠)作为前缀,这表示应将后面的字符当作普通字符,你也可以使用提供的预定义模式之一。To use these characters, either prefix the character with a \ (backslash) to indicate that the character should be taken literally or use one of the predefined patterns. 下表列出了这些特殊字符:This table lists the special characters:

特殊字符Special character 说明Description
.. 圆点或句点dot or period
?? 问号question mark
** 星号asterisk
+ 加号plus
( )( ) 圆括号parenthesis
[ ][ ] 方括号square brackets
{ }{ } 大括号curly braces
^ 脱字号caret
$ 美元符号dollar sign
|| 竖线vertical bar or pipe
\ 反斜杠backslash

例如,可以For example, you can match "Hello?" 使用在问号前加一个反斜杠的模式 "Hello\?"by using the pattern "Hello\?" 来匹配“Hello?”。with a backslash before the question mark.

预定义模式Predefined patterns

预定义模式提供了一种简单方法来匹配一组字符中的一个字符或一个多字符序列。Predefined patterns provide a simple way to match one of a set of characters, or a sequence of multiple characters. 使用字符串连接运算符 & 可将你自己的文本字符串与 Match 枚举中的模式进行组合:Use the string concatenation operator & to combine your own text strings with members of the Match enum:

Match 枚举Match Enum 说明Description 正则表达式Regular Expression
AnyAny 匹配任何字符。Matches any character. ..
CommaComma 匹配逗号。Matches a comma. ,,
DigitDigit 匹配单个数字(“0”到“9”)。Matches a single digit ("0" through "9"). \d\d
EmailEmail 匹配包含“@”符号和包含点(“.”)的域名的电子邮件地址。Matches an email address that contains an "at" symbol ("@") and a domain name that contains a dot (".") .+@.+\.[^\.]{2,}.+@.+\.[^\.]{2,}
HyphenHyphen 匹配连字符。Matches a hyphen. \-
LeftParenLeftParen 匹配左圆括号“(”。Matches a left parenthesis "(". \(\(
LetterLetter 匹配一个字母。Matches a letter. \p{L}\p{L}
MultipleDigitsMultipleDigits 匹配一个或多个数字。Matches one or more digitis. \d+\d+
MultipleLettersMultipleLetters 匹配一个或多个字母。Matches one or more letters. \p{L}+\p{L}+
MultipleNonSpacesMultipleNonSpaces 匹配不包含空白(空格、制表符、换行符)的一个或多个字符。Matches one or more characters that don't add whitespace (space, tab, newline). \S+\S+
MultipleSpacesMultipleSpaces 匹配可间断(包含空格、制表符、换行符)的一个或多个字符。Matches one or more characters that add whitespace (space, tab, newline). \s+\s+
NonSpaceNonSpace 匹配不包含空白的单个字符。Matches a single character that doesn't add whitespace. \S\S
OptionalDigitsOptionalDigits 配零个、一个或多个数字。Matches zero, one, or more digits. \d\d
OptionalLettersOptionalLetters 匹配零个、一个或多个字母。Matches zero, one, or more letters. \p{L}\p{L}
OptionalNonSpacesOptionalNonSpaces 匹配不包含空白的零个、一个或多个字符。Matches zero, one, or more characters that don't add whitespace. \S\S
OptionalSpacesOptionalSpaces 匹配包含空白的零个、一个或多个字符。Matches zero, one, or more characters that add whitespace. \s\s
PeriodPeriod 匹配句点或圆点(“.”)。Matches a period or dot ("."). \.\.
RightParenRightParen 匹配右圆括号“)”。Matches a right parenthesis ")". \)\)
SpaceSpace 匹配包含空白的字符。Matches a character that adds whitespace. \s\s

例如,模式 "A" & MultipleDigits 与字母“A”后跟一个或多个数字的形式匹配。For example, the pattern "A" & MultipleDigits will match the letter "A" followed by one or more digits.

正则表达式Regular expressions

IsMatch 函数使用的模式是正则表达式The pattern used by IsMatch is a regular expression. 上述普通字符和预定义模式可以帮助你构建正则表达式。The ordinary characters and predefined patterns that are described above help build regular expressions.

正则表达式的功能非常强大,许多编程语言中都有提供,并且用途广泛。Regular expressions are very powerful, available in many programming languages, and used for a wide variety of purposes. 本文无法详细介绍正则表达式的每个方面,不过网络上有大量的信息和教程供你参考。This article can't describe all aspects of regular expressions, but a wealth of information and tutorials are published on the web to aid you.

正则表达式有不同的方言,PowerApps 使用 JavaScript 方言的一种变体。Regular expressions have different dialects, and PowerApps uses a variant of the JavaScript dialect. 有关详细信息,请参阅正则表达式语法For more information, see regular expression syntax.

在上述 Match 枚举表中,每个枚举都可以扩展为一个正则表达式,“正则表达式”列中的文本字符串就是相应的定义。In the Match enum table above, each enum expands into a regular expression, and the text string in the "Regular Expression" column defines that expression.

匹配选项Match options

你可以通过指定一个或多个选项(可使用字符串连接运算符 (&) 进行组合)修改 IsMatch 函数的行为。You can modify the behavior of IsMatch by specifying one or more options, which you can combine by using the string concatenation operator (&).

默认情况下,IsMatch 函数会检测整个文本字符串是否完全匹配。By default, IsMatch tests for a complete match of the entire text string.

MatchOptions 枚举MatchOptions Enum 说明Description 对正则表达式的影响Impact on regular expression
BeginsWithBeginsWith 模式必须与文本的开头匹配。The pattern must match from the beginning of the text. 在正则表达式的开头添加 ^Adds a ^ to the start of the regular expression.
CompleteComplete 默认值。Default. 模式必须与整个文本从头到尾完全匹配。The pattern must match the entire text, from beginning to end. 在正则表达式的开头添加 ^,并且在末尾添加 $Adds a ^ to the start and $ to the end of the regular expression.
ContainsContains 模式必须出现在文本中,但不必与开头或结尾匹配。The pattern must appear somewhere in the text but doesn't need to begin or end it. 不修改正则表达式。Doesn't modify the regular expression.
EndsWithEndsWith 模式必须与文本的结尾匹配。The pattern must match the end of the text. 在正则表达式的末尾添加 $Adds a $ to the end of the regular expression.
IgnoreCaseIgnoreCase 匹配字母时不区分大小写。Treats the matching of letters in a case-insensitive manner. 默认情况下,匹配时区分大小写。By default, matching is case sensitive. 不修改正则表达式。Doesn't modify the regular expression.
MultilineMultiline 匹配多行。Matches across multiple lines. 不修改正则表达式。Doesn't modify the regular expression.

语法Syntax

IsMatch( Text, Pattern [, Options ] )IsMatch( Text, Pattern [, Options ] )

  • Text – 必需。Text – Required. 要测试的文本字符串。The text string to test.
  • Pattern – 必需。Pattern – Required. 要作为文本字符串测试的模式。The pattern to test, as a text string. 连接 Match 枚举定义的预定义模式或提供正则表达式。Concatenate predefined patterns that the Match enum defines or provide a regular expression.
  • Options – 可选。Options – Optional. MatchOptions 枚举值的文本字符串组合。A text string combination of MatchOptions enum values. 默认情况下,使用 MatchOptions.CompleteBy default, MatchOptions.Complete is used.

示例Examples

普通字符Ordinary characters

假设应用包含一个名为 TextInput1 的“文本输入”控件:Imagine that your app contains a Text input control named TextInput1. 用户在这个控件中输入的值存储在数据库中。The user enters values into this control to be stored in a database.

用户在 TextInput1 中输入 Hello worldThe user types Hello world into TextInput1.

公式Formula 说明Description 结果Result
IsMatch( TextInput1.Text, "Hello world" )IsMatch( TextInput1.Text, "Hello world" ) 测试用户输入的内容是否与字符串“Hello world”完全匹配。Tests whether the user's input matches, exactly, the string "Hello world" truetrue
IsMatch( TextInput1.Text, "Good bye" )IsMatch( TextInput1.Text, "Good bye" ) 测试用户输入的内容是否与字符串“Good bye”完全匹配。Tests whether the user's input matches, exactly, the string "Good bye" falsefalse
IsMatch( TextInput1.Text, "hello", Contains )IsMatch( TextInput1.Text, "hello", Contains ) 测试用户输入的内容是否包含单词“hello”(区分大小写)。Tests whether the user's input contains the word "hello" (case sensitive). falsefalse
IsMatch( TextInput1.Text, "hello", Contains & IgnoreCase )IsMatch( TextInput1.Text, "hello", Contains & IgnoreCase ) 测试用户输入的内容是否包含单词“hello”(不区分大小写)。Tests whether the user's input contains the word "hello" (case insensitive). truetrue

预定义模式Predefined patterns

公式Formula 说明Description 结果Result
IsMatch( "123-45-7890", Digit & Digit & Digit & Hyphen & Digit & Digit & Hyphen & Digit & Digit & Digit & Digit & Digit )IsMatch( "123-45-7890", Digit & Digit & Digit & Hyphen & Digit & Digit & Hyphen & Digit & Digit & Digit & Digit & Digit ) 匹配美国社会安全号码。Matches a United States Social Security Number truetrue
IsMatch( "joan@contoso.com", Email )IsMatch( "joan@contoso.com", Email ) 匹配电子邮件地址。Matches an email address truetrue
IsMatch( "123.456", MultipleDigits & Period & OptionalDigits )IsMatch( "123.456", MultipleDigits & Period & OptionalDigits ) 匹配一个数字序列,后跟一个句点,后面再跟零个或多个数字。Matches a sequence of digits, a period, and then zero or more digits. truetrue
IsMatch( "123", MultipleDigits & Period & OptionalDigits )IsMatch( "123", MultipleDigits & Period & OptionalDigits ) 匹配一个数字序列,后跟一个句点,后面再跟零个或多个数字。Matches a sequence of digits, a period, and then zero or more digits. 文本中没有句点,因此该模式不匹配。A period doesn't appear in the text, so this pattern isn't matched. falsefalse

正则表达式Regular expressions

公式Formula 说明Description 结果Result
IsMatch( "986", "\d+" )IsMatch( "986", "\d+" ) 匹配大于零的整数。Matches a an integer greater than zero. truetrue
IsMatch( "1.02", "\d+(.\d\d)?" )IsMatch( "1.02", "\d+(.\d\d)?" ) 匹配正货币金额。Matches a positive currency amount. 如果输入的内容包含小数点,则小数点后还要有 2 个数字字符。If the input contains a decimal point, the input must also contain 2 numeric characters after the decimal point. 例如,3.00 有效,但 3.1 无效。For example, 3.00 is valid, but 3.1 isn't. truetrue
IsMatch( "-4.95", "(-)?\d+(.\d\d)?" )IsMatch( "-4.95", "(-)?\d+(.\d\d)?" ) 匹配正或负货币金额。Matches a positive or negative currency amount. 如果输入的内容包含小数点,则小数点后还要有 2 个数字字符。If the input contains a decimal point, the input must also contain 2 numeric characters after the decimal point. truetrue
IsMatch( "111-11-1111", "\d{3}-\d{2}-\d{4}" )IsMatch( "111-11-1111", "\d{3}-\d{2}-\d{4}" ) 匹配美国社会安全号码。Matches a United States Social Security number. 验证提供的输入字段的格式、类型和长度。Validates the format, type, and length of the supplied input field. 要匹配的字符串必须包含 3 个数字字符,后跟一个短划线,再跟 2 个数字字符,再跟一个短划线,最后跟 4 个数字字符。The string to match must consist of 3 numeric characters followed by a dash, then 2 numeric characters followed by a dash, and then 4 numeric characters. truetrue
IsMatch( "111-111-111", "\d{3}-\d{2}-\d{4}" )IsMatch( "111-111-111", "\d{3}-\d{2}-\d{4}" ) 与上例相同,但其中一个连字符的位置不对。Same as the previous example, but one of the hyphens is out of place in the input. falsefalse
IsMatch( "weakpassword", "(?!^[0-9]*$)(?!^[a-zA-Z]*$)([a-zA-Z0-9]{8,10})" )IsMatch( "weakpassword", "(?!^[0-9]*$)(?!^[a-zA-Z]*$)([a-zA-Z0-9]{8,10})" ) 验证强密码,除包含至少一个数字和至少一个字母字符外,还必须包含 8、9 或 10 个字符。Validates a strong password, which must contain 8, 9, or 10 characters, in addition to at least one digit and at least one alphabetic character. 字符串不能包含特殊字符。The string must not contain special characters. falsefalse
IsMatch( "http://microsoft.com", "(ht|f)tp(s?)://[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(/?)([a-zA-Z0-9-.?,'/\+&%$#_]*)?" )IsMatch( "http://microsoft.com", "(ht|f)tp(s?)://[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(/?)([a-zA-Z0-9-.?,'/\+&%$#_]*)?" ) 验证 http、https 或 ftp URL。Validates an http, https, or ftp URL. truetrue