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.
A lookahead assertion checks for a pattern ahead of the current position in the string.
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".
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".
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.
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".
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".
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.
An introduction to regular expression capture groups, and how to use them in C++ with regex search
, replace
, iterator
, and token_iterator