Guest User

5.Регулярни изрази

a guest
Feb 24th, 2016
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. Регулярни изрази
  2.  
  3. Регулярен израз – последователност от символи, които формират някаква шаблон, по който се търси
  4.  
  5. [character_group] – matches any single character in character_group
  6. [^] - negation
  7. [first-last] – character range: matches any single character in the range from first to last (according to the ASCII table)
  8. \w – matches any word character (a-z, A-Z, 0-9, _)
  9. \W - matches any non-word character – the opposite to \w
  10. \s – matches any whitespace character (\r\n, \t, ‘ ‘)
  11. \S – matches any non-whitespace character
  12. \d – matches any digit character
  13. \D – matches any non-digit character
  14.  
  15. * - matches the previous element zero or more times (as many times as possible)
  16. + - matches the previous element one or more times (as many times as possible)
  17. ? - matches the previous element zero or one time
  18. +? – matches between one and unlimited times (as few times as possible) – lazy
  19. *? – matches between zero and unlimited times (as few times as possible) – lazy
  20.  
  21. {n} - matches the previous element exactly n times
  22. {n,} - matches the previous element at least n times
  23. {n,m}- matches the previous element at least n times, but no more than m times
  24.  
  25. ^ - the match must start at the beginning of the string or line
  26. $ - the match must occur at the end of the string or before \n
  27.  
  28. \b – the match must occur on a boundary between a \w(alphanumeric) and a \W(non-alphanumeric) character
  29. \B – the match must not occur on a boundary between a \w(alphanumeric) and a \W(non-alphanumeric) character
  30.  
  31.  
  32. (subexpression) – captures the matched subexpression and assigns it a number
  33. (?<name>subexpression) – captures the matched subexpression into a named group
  34. (?:subexpression) – defines a non-capturing group
  35. . – matches any character except new line
  36.  
  37. var regex = new Regex(string pattern);
  38. regex.Match(string text) – returns the first match that corresponds to the pattern
  39. regex.Matches(string text) – returns a collection of matching strings that correspond to the pattern
  40. regex.Replace(string text, string replacement) – replaces all strings that match the pattern with the provided replacement
  41.  
  42. !! look-behinds are more limited than look-aheads, because they do not support quantifiers of varying size such as “*”, “?”, “+”
  43.  
  44. (не работи в C#) The escape sequence \K is similar to a look-behind assertion because it causes any previously-matched characters to be omitted from the final matched string. For example, foo\Kbar matches "foobar" but reports that it has matched "bar".
Add Comment
Please, Sign In to add comment