Regex::IsMatch Method (String, Int32)
Indicates whether the regular expression specified in the Regex constructor finds a match in the specified input string, beginning at the specified starting position in the string.
Assembly: System (in System.dll)
Parameters
- input
- Type: System::String
The string to search for a match.
- startat
- Type: System::Int32
The character position at which to start the search.
| Exception | Condition |
|---|---|
| ArgumentNullException | input is nullptr. |
| ArgumentOutOfRangeException | startat is less than zero or greater than the length of input. |
The IsMatch method is typically used to validate a string or to ensure that a string conforms to a particular pattern without retrieving that string for subsequent manipulation. To determine whether one or more strings match a regular expression pattern and to retrieve them for subsequent manipulation, call the Match or Matches method.
The following example illustrates the use of the IsMatch(String, Int32) method to determine whether a string is a valid part number. It searches for a part number that follows a colon (:) character in a string. The IndexOf(Char) method is used to determine the position of the colon character, which is then passed to the IsMatch(String, Int32) method. The regular expression assumes that the part number has a specific format that consists of three sets of characters separated by hyphens. The first set, which contains four characters, must consist of an alphanumeric character followed by two numeric characters followed by an alphanumeric character. The second set, which consists of three characters, must be numeric. The third set, which consists of four characters, must have three numeric characters followed by an alphanumeric character.
The regular expression pattern is:
[a-zA-Z0-9]\d{2}[a-zA-Z0-9](-\d{3}){2}[A-Za-z0-9]$
The following table shows how the regular expression pattern is interpreted.
Pattern | Description |
|---|---|
[a-zA-Z0-9] | Match a single alphabetic character (a through z or A through Z) or numeric character. |
\d{2} | Match two numeric characters. |
[a-zA-Z0-9] | Match a single alphabetic character (a through z or A through Z) or numeric character. |
- | Match a hyphen. |
\d{3} | Match exactly three numeric characters. |
(-\d{3}){2} | Find a hyphen followed by three numeric characters, and match two occurrences of this pattern. |
[a-zA-Z0-9] | Match a single alphabetic character (a through z or A through Z) or numeric character. |
$ | End the match at the end of the line. |
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.