faq.qbk 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. [/
  2. Copyright 2006-2007 John Maddock.
  3. Distributed under the Boost Software License, Version 1.0.
  4. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt).
  6. ]
  7. [section:faq FAQ]
  8. [*Q.] I can't get regex++ to work with escape characters, what's going on?
  9. [*A.] If you embed regular expressions in C++ code, then remember that escape
  10. characters are processed twice: once by the C++ compiler, and once by the
  11. Boost.Regex expression compiler, so to pass the regular expression \d+
  12. to Boost.Regex, you need to embed "\\d+" in your code. Likewise to match a
  13. literal backslash you will need to embed "\\\\" in your code.
  14. [*Q.] No matter what I do regex_match always returns false, what's going on?
  15. [*A.] The algorithm regex_match only succeeds if the expression matches *all*
  16. of the text, if you want to *find* a sub-string within the text that matches
  17. the expression then use regex_search instead.
  18. [*Q.] Why does using parenthesis in a POSIX regular expression change the
  19. result of a match?
  20. [*A.] For POSIX (extended and basic) regular expressions, but not for perl regexes,
  21. parentheses don't only mark; they determine what the best match is as well.
  22. When the expression is compiled as a POSIX basic or extended regex then Boost.Regex
  23. follows the POSIX standard leftmost longest rule for determining what matched.
  24. So if there is more than one possible match after considering the whole expression,
  25. it looks next at the first sub-expression and then the second sub-expression
  26. and so on. So...
  27. "'''(0*)([0-9]*)'''" against "00123" would produce
  28. $1 = "00"
  29. $2 = "123"
  30. where as
  31. "0*([0-9])*" against "00123" would produce
  32. $1 = "00123"
  33. If you think about it, had $1 only matched the "123", this would be "less good"
  34. than the match "00123" which is both further to the left and longer. If you
  35. want $1 to match only the "123" part, then you need to use something like:
  36. "0*([1-9][0-9]*)"
  37. as the expression.
  38. [*Q.] Why don't character ranges work properly (POSIX mode only)?
  39. [*A.] The POSIX standard specifies that character range expressions are
  40. locale sensitive - so for example the expression [A-Z] will match any
  41. collating element that collates between 'A' and 'Z'. That means that for
  42. most locales other than "C" or "POSIX", [A-Z] would match the single
  43. character 't' for example, which is not what most people expect - or
  44. at least not what most people have come to expect from regular
  45. expression engines. For this reason, the default behaviour of Boost.Regex
  46. (perl mode) is to turn locale sensitive collation off by not setting the
  47. `regex_constants::collate` compile time flag. However if you set a non-default
  48. compile time flag - for example `regex_constants::extended` or
  49. `regex_constants::basic`, then locale dependent collation will be enabled,
  50. this also applies to the POSIX API functions which use either
  51. `regex_constants::extended` or `regex_constants::basic` internally.
  52. [Note - when `regex_constants::nocollate` in effect, the library behaves
  53. "as if" the LC_COLLATE locale category were always "C", regardless of what
  54. its actually set to - end note].
  55. [*Q.] Why are there no throw specifications on any of the functions?
  56. What exceptions can the library throw?
  57. [*A.] Not all compilers support (or honor) throw specifications, others
  58. support them but with reduced efficiency. Throw specifications may be added
  59. at a later date as compilers begin to handle this better. The library
  60. should throw only three types of exception: [boost::regex_error] can be
  61. thrown by [basic_regex] when compiling a regular expression, `std::runtime_error`
  62. can be thrown when a call to `basic_regex::imbue` tries to open a message
  63. catalogue that doesn't exist, or when a call to [regex_search] or [regex_match]
  64. results in an "everlasting" search, or when a call to `RegEx::GrepFiles` or
  65. `RegEx::FindFiles` tries to open a file that cannot be opened, finally
  66. `std::bad_alloc` can be thrown by just about any of the functions in this library.
  67. [*Q.] Why can't I use the "convenience" versions of regex_match /
  68. regex_search / regex_grep / regex_format / regex_merge?
  69. [*A.] These versions may or may not be available depending upon the
  70. capabilities of your compiler, the rules determining the format of
  71. these functions are quite complex - and only the versions visible to
  72. a standard compliant compiler are given in the help. To find out
  73. what your compiler supports, run <boost/regex.hpp> through your
  74. C++ pre-processor, and search the output file for the function
  75. that you are interested in. Note however, that very few current
  76. compilers still have problems with these overloaded functions.
  77. [endsect]