Regular expressions in C#
The regular expressions language is designed specifically for string processing. NET also offers some very sophisticated classes that deal withsituations in which you need to identify all words that begin with “ s ” and contain at least one “ n,” or strings that adhere to an employee ID or a Social Security number construction. You can write methods to perform this kind of processing using the String class, such methods are cumbersome to write. Instead, you will find that some classes, specifically those from System.Text.RegularExpressions, are designed to perform this kind of processing.It contains two features:
1)A set of escape codes for identifying specific types of characters. You will be familiar with the use ofthe * character to represent any substring in DOS expressions. (For example, the DOS command
Dir Re* lists the files with names beginning with Re.) Regular expressions use many sequences likethis to represent items such as any one character, a word break, one optional character, and so on.
2)A system for grouping parts of substrings and intermediate results during a search operation.With regular expressions, you can perform quite sophisticated and high - level operations on strings. Forexample, you can:
-Identify all repeated words in a string
-Convert all words to title case
-Convert all words longer than three characters to title case
-Separate the various elements of a URI
The following table lists some of the main special characters or escape sequences that you can use.
symbol
| Meaning
| Expl
| Matches
| ^
| Beginning of input text
| ^B
| B, but only if first character in text
| $
| End of input text
| X$
| X, but only if last character in text
| .
| Any single character except the
newline character (\)
| i.ation
| isation, ization
|
Дата добавления: 2015-09-18 | Просмотры: 612 | Нарушение авторских прав
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
|