Search Options






Search Options

namespace regex_constants {
  static const match_flag_type
    match_default,
    match_not_bol,
    match_not_eol,
    match_not_bow,
    match_not_eow,
    match_any,
    match_not_null,
    match_continuous,
    natch_prev_avail ;
}

Values of type match_flag_type can be combined with the logical OR operator and passed to the various search algorithms. Flags that are not applicable to a particular grammar are ignored when that grammar was used to create the regular expression object used for the search. The flag values have the following meanings:

  • match_default: use normal matching rules.

  • match_not_bol: do not treat the first position in the target sequence as the beginning of a line.

  • match_not_eol: do not treat the last position in the target sequence as the end of a line.

  • match_not_bow: do not treat the first position in the target sequence as the beginning of a word.

  • match_not_eow: do not treat the last position in the target sequence as the end of a word.

  • match_any: if more than one match is possible, any match is acceptable.

  • match_not_null: do not treat an empty sequence as a match.

  • match_continuous: do not search for matches other than at the beginning of the target sequence.

  • match_prev_avail: when an algorithm is called with a target sequence defined by a pair of iterators first and last, --first is a valid iterator; ignore match_not_bol and match_not_bow.

These flag values can be passed to the algorithms regex_match, regex_search, and regex_replace[1] to change the matching rules.

[1] Discussed in Chapter 20.

17.4.1. The match_default Flag

The flag designates the default matching rules for the grammar that was used to create the regular expression object used for the search. All of the search algorithms have a default value of match_default for their flags argument, so you often won't have to pass this flag to the algorithms.[2]

[2] Technically, match_default is required to have a value of 0, so it can be combined with any of the other flags without changing their meanings.

17.4.2. The match_not_bol Flag

Ordinarily, the beginning of the target sequence is treated as the beginning of a line. This flag tells the search engine not to apply this rule. That's useful in repetitive searches that resume where the previous search finished. We look at repetitive searches in Chapter 19. In the meantime, this example shows how this flag works.

Figure. Flag match_not_bol (regexsrch/notbol.cpp)

#include <regex>
#include <iostream>
using std::tr1::regex; using std::tr1::regex_match;
using namespace std::tr1::regex_constants;
using std::cout;

int main()
  { // demonstrate use of match_not_bol
  regex rgx("^abcd");
  const char *tgt = "abcd";
  if (regex_match(tgt, rgx))
    cout << "Matches .\n";
  else
    cout << "Doesn' t match .\n";
  if (regex_match (tgt, rgx, match_not_bol))
    cout << "Matches .\n";
  else
    cout << "Doesn' t match .\n";
  return 0;
  }

The first search succeeds because the beginning of the target sequence is treated as the beginning of a line, thus matching the initial character '^'. The second search fails because the beginning of the target sequence is not treated as the beginning of a line.

17.4.3. The match_not_eol Flag

Ordinarily, the end of the target sequence is treated as the end of a line. This flag tells the search engine not to apply this rule. That's useful when searching through buffered input, where additional input might not yet have been read into the buffer.

Figure. Flag match_not_eol (regexsrch/noteol.cpp)

#include <regex>
#include <iostream>
using std::tr1::regex; using std::tr1::regex_match;
using namespace std::tr1::regex_constants;
using std::cout;

int main()
  { // demonstrate use of match_not_eol
  regex rgx ("abcd$");
  const char *txt = "abcde";
  const char *end = txt + 4;        // points to 'e'
  if (regex_match(txt, end, rgx))
    cout << "Matches .\n";
  else
    cout << "Doesn' t match .\n";
  if (regex_match (txt, end, rgx, match_not_eol))
    cout << "Matches .\n";
  else
    cout << "Doesn't match .\n";
  return 0;
  }

The first search succeeds because the end of the target sequence is treated as the end of a line, thus matching the final character '$'. The second search fails because the end of the target sequence is not treated as the end of a line.

17.4.4. The match_not_bow Flag

Ordinarily, the beginning of the target sequence is treated as the beginning of a word if the first character can be part of a word. This flag tells the search engine not to apply this rule. That's useful in repetitive searches that resume where the previous search finished. We look at repetitive searches in Chapter 19. In the meantime, this example shows how this flag works.

Figure. Flag match_not_bow (regexsrch/notbow.cpp)

#include <regex>
#include <iostream>
using std::tr1::regex; using std::tr1::regex_match;
using namespace std::tr1::regex_constants;
using std::cout;

int main()
  { // demonstrate use of match_not_bow
  regex rgx ("\\ babcd");
  const char *tgt = "abcd";
  if (regex_match (tgt, rgx))
    cout << "Matches .\n";
  else
    cout << "Doesn 't match .\n";
  if (regex_match (tgt, rgx, match_not_bow))
    cout << "Matches .\n";
  else
    cout << "Doesn' t match .\n";
  return 0;
  }

The first search succeeds because the beginning of the target sequence is treated as the beginning of a word, thus matching the initial escape sequence "\b". The second search fails because the beginning of the target sequence is not treated as the beginning of a word.

17.4.5. The match_not_eow Flag

Ordinarily, the end of the target sequence is treated as the end of a word if the last character can be part of a word. This flag tells the search engine not to apply this rule. That's useful when searching through buffered input, where additional input might not yet have been read into the buffer.

Figure. Flag match_not_eow (regexsrch/noteow.cpp)

#include <regex>
#include <iostream>
using std::tr1::regex; using std::tr1::regex_match;
using namespace std::tr1::regex_constants;
using std::cout;

int main()
  { // demonstrate use of match_not_eow
  regex rgx ("abcd \\ b");
  const char *txt = "abcde";
  const char *end = txt + 4;        // points to 'e'
  if (regex_match (txt, end, rgx))
    cout << "Matches .\n";
  else
    cout << "Doesn' t match .\n";
  if (regex_match (txt, end, rgx, match_not_eow))
    cout << "Matches .\n";
  else
    cout << "Doesn' t match .\n";
  return 0;
  }

The first search succeeds because the end of the target sequence is treated as the end of a word, thus matching the final escape sequence "\b". The second search fails because the end of the target sequence is not treated as the end of a word.

17.4.6. The match_any Flag

For grammars other than ECMAScript, when a regular expression includes an alternation, a successful match should use the longest possible alternative. For example, when matching the regular expression "(wee|week).*" to the target sequence "weeknights", a successful match will associate the text "week" with the first capture group, because that's a longer match than "wee". This means that even after finding that the first alternative, "wee", leads to a successful match, the search engine must continue, in order to find the match that uses the longer alternative. This flag tells the search engine not to apply that rule. Any successful match is acceptable, even if a longer branch of an alternative would lead to a match with the same overall length.

This flag does not affect the requirement of finding the longest possible match. It simplifies the rule for breaking ties among equally long matches, by not requiring repeated searches for the longest leftmost alternative. We look at examples using this flag in Chapter 18.

17.4.7. The match_not_null Flag

This flag tells the search engine not to consider a match that matches zero characters. That's useful in repetitive searches that resume where the previous search left off. If the previous search matched zero characters, it left off at the place where it started, and repeating the search would simply find the zero-length match again. In order to make progress, a repetitive search should use the match_not_null flag after a zero-length match. We look at repetitive searches in Chapter 19. In the meantime, this example shows how this flag works.

Figure. Flag match_not_null (regexsrch/notnull.cpp)

#include <regex>
#include <iostream>
using std::tr1::regex; using std::tr1::regex_match;
using namespace std::tr1::regex_constants;
using std::cout;

int main()
  { // demonstrate use of match_not_null
  regex rgx ("a *| b");
  const char *tgt = "ccc";
  if (regex_search (tgt, rgx))
    cout << "Matches .\n";
  else
    cout << "Doesn' t match .\n";
  if (regex_search (tgt, rgx, match_not_null))
    cout << "Matches .\n";
  else
    cout << "Doesn' t match .\n";
  return 0;
  }

In the first call to regex_search, the first alternative in the regular expression, "a*", successfully matches zero characters at the start of the target sequence. When the search is repeated with the match_not_null flag, it fails because this match is not allowed.

17.4.8. The match_continuous Flag

This flag tells the search engine to consider only matches beginning at the start of the target sequence. This doesn't affect the result of calling regex_match, which matches the entire target sequence. This flag can change the result of calling regex_search.

Figure. Flag match_continuous (regexsrch/continuous.cpp)

#include <regex>
#include <iostream>
using std::tr1::regex; using std::tr1::regex_search;
using namespace std::tr1::regex_constants;
using std::cout;

int main()
  { // demonstrate use of match_continuous
  regex rgx ("bcd");
  const char *tgt = "abcd";
  if (regex_search (tgt, rgx))
    cout << "Matches .\n";
  else
    cout << "Doesn' t match .\n";
  if (regex_search (tgt, rgx, match_continuous))
    cout << "Matches .\n";
  else
    cout << "Doesn' t match .\n";
  return 0;
  }

The first search succeeds because the regular expression "bcd" matches the end of the target sequence. The second search fails because the match is required to begin at the beginning of the target sequence.

17.4.9. The match_prev_avail Flag

When passed to a search that uses a pair of iterators to designate the target sequence, this flag tells the search engine that the position preceding the first iterator is a valid element. This is needed when a search begins after the beginning of a sequence, so that the special rules for the first character in the target sequence won't be applied.

Figure. Flag match_prev_avail (regexsrch/prevavail.cpp)

#include <regex>
#include <iostream>
using std::tr1::regex; using std::tr1::regex_match;
using namespace std::tr1::regex_constants;
using std::cout;
int main()
  { // demonstrate use of match_prev_avail
  regex rgx ("\\ bbcd");
  const char *txt = "abcd";
  const char *tgt = txt + 1;
  if (regex_match (tgt, rgx))
    cout << "Matches .\n";
  else
    cout << "Doesn' t match .\n";
  if ( regex_match (tgt, rgx, match_prev_avail))
    cout << "Matches .\n";
  else
    cout << "Doesn' t match .\n";
  return 0;
  }

The first match succeeds because the search engine treats the character 'b' in the target sequence as the beginning of a word. The second match fails because the search engine looks back one character and sees that the character 'b' in the target sequence is not the beginning of a word.



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows