Regex Capture Groups

Lookahead and Lookbehind

What are lookahead and lookbehind assertions in regex?

Abstract art representing computer programming

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.

Answers to questions are automatically generated and may not have been reviewed.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 125 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved