Lookahead and Lookbehind

What are lookahead and lookbehind assertions in regex?

Lookahead and lookbehind assertions in regex allow you to match a pattern only if it is followed or preceded by another pattern, without including the surrounding pattern in the match.

Lookahead Assertions

A lookahead assertion checks for a pattern ahead of the current position in the string.

Positive Lookahead

A positive lookahead ensures that the specified pattern exists after the current position:

#include <iostream>
#include <regex>

int main() {
  std::string input = "foo123";
  std::regex pattern("foo(?=123)");
  std::smatch match;

  if (std::regex_search(input, match, pattern)) {
    std::cout << "Match: " << match.str();
  }
}
Match: foo

Here, foo(?=123) matches "foo" only if it is followed by "123".

Negative Lookahead

A negative lookahead ensures that the specified pattern does not exist after the current position:

#include <iostream>
#include <regex>

int main() {
  std::string input = "foo123";
  std::regex pattern("foo(?!456)");
  std::smatch match;

  if (std::regex_search(input, match, pattern)) {
    std::cout << "Match: " << match.str();
  }
}
Match: foo

Here, foo(?!456) matches "foo" only if it is not followed by "456".

Lookbehind Assertions

A lookbehind assertion checks for a pattern behind the current position in the string. The standard C++ regex library does not support lookbehind assertions.

To use lookbehind assertions in C++, you will need to use a different library that supports it. One such library is Boost.Regex.

Positive Lookbehind

A positive lookbehind ensures that the specified pattern exists before the current position. Here's an example using Boost.Regex:

#include <iostream>
#include <boost/regex.hpp>

int main() {
  std::string input = "123foo";
  boost::regex pattern("(?<=123)foo");
  boost::smatch match;

  if (boost::regex_search(input, match, pattern)) {
    std::cout << "Match: " << match.str();
  } else {
    std::cout << "No match found.";
  }
}
Match: foo

Here, (?<=123)foo matches "foo" only if it is preceded by "123".

Negative Lookbehind

A negative lookbehind ensures that the specified pattern does not exist before the current position. Here's an example using Boost.Regex:

#include <iostream>
#include <boost/regex.hpp>

int main() {
  std::string input = "123foo";
  boost::regex pattern("(?<!456)foo");
  boost::smatch match;

  if (boost::regex_search(input, match, pattern)) {
    std::cout << "Match: " << match.str() << "\n";
  } else {
    std::cout << "No match found.\n";
  }
}
Match: foo

Here, (?<!456)foo matches "foo" only if it is not preceded by "456".

Summary

Lookahead and lookbehind assertions are powerful tools in regex for conditional pattern matching. They allow you to enforce the presence or absence of surrounding patterns without including those patterns in the match.

Regex Capture Groups

An introduction to regular expression capture groups, and how to use them in C++ with regex_search, regex_replace, regex_iterator, and regex_token_iterator

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Greedy vs Lazy Quantifiers
What are the differences between greedy and lazy quantifiers in regex?
Non-Capture Groups
What are non-capture groups and when should they be used?
Regex Replace in C++
How do you replace text in a string using regex in C++?
Splitting Strings with Regex
Can regex be used to split strings in C++?
Counting Regex Matches
How can you count the number of matches found in a string using regex?
Using Backreferences in Regex
How do you use backreferences in C++ regex?
Formatting Dates with Regex
How can you use regex to format dates in C++?
Third-Party Regex Libraries
Are there any recommended third-party libraries for working with regex in C++?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant