{"id":2691,"date":"2021-09-14T11:30:23","date_gmt":"2021-09-14T11:30:23","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=2691"},"modified":"2025-04-07T13:19:04","modified_gmt":"2025-04-07T13:19:04","slug":"regex-lookahead","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/regex-lookahead\/","title":{"rendered":"Regex Lookahead"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn to use the regex lookahead and negative lookahead.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-regex-lookahead'>Introduction to the regex lookahead <a href=\"#introduction-to-the-regex-lookahead\" class=\"anchor\" id=\"introduction-to-the-regex-lookahead\" title=\"Anchor for Introduction to the regex lookahead\">#<\/a><\/h2>\n\n\n\n<p>Sometimes, you want to match <code>A<\/code> but only if it is followed by <code>B<\/code>. For example, suppose you have the following string:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-number\">2<\/span> chicken weigh <span class=\"hljs-number\">30<\/span>lb<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And you want to match the number (<code>30<\/code>) followed by the string <code>lb<\/code>, not the number <code>2<\/code>. In this case, you can use the regex lookahead with the following syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">A(?=B)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The lookahead means to search for <code>A<\/code> but matches only if followed by <code>B<\/code>. For a number followed by the string <code>lb<\/code>, you can use the following pattern:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">\\d+(?=lb)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this pattern:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>\\d+<\/code> match one or more digits<\/li>\n\n\n\n<li><code>?=<\/code> is the lookahead<\/li>\n\n\n\n<li><code>lb<\/code> match the text <code>lb<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>The following code uses the regex lookahead syntax to match a number followed by the text <code>lb<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$pattern = <span class=\"hljs-string\">'\/\\d+(?=lb)\/'<\/span>;\n$str = <span class=\"hljs-string\">'2 chicken weigh 30lb'<\/span>;\n\n<span class=\"hljs-keyword\">if<\/span> (preg_match($pattern, $str, $matches)) {\n    print_r($matches); <span class=\"hljs-comment\">\/\/ 30<\/span>\n\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRwYXR0ZXJuID0gJy9cZCsoPz1sYikvJzsKJHN0ciA9ICcyIGNoaWNrZW4gd2VpZ2ggMzBsYic7CgppZiAocHJlZ19tYXRjaCgkcGF0dGVybiwgJHN0ciwgJG1hdGNoZXMpKSB7CiAgICBwcmludF9yKCRtYXRjaGVzKTsgLy8gMzAKCn0\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">Array<\/span>\n(\n    &#91;<span class=\"hljs-number\">0<\/span>] =&gt; <span class=\"hljs-number\">30<\/span>\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following regular expression also matches <code>30<\/code> followed immediately by <code>lb<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-string\">'\/\\d+(?=lb)\/'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$pattern = <span class=\"hljs-string\">'\/\\d+(?=lb)\/'<\/span>;\n$str = <span class=\"hljs-string\">'2 chicken weigh 30lb'<\/span>;\n\n<span class=\"hljs-keyword\">if<\/span> (preg_match($pattern, $str, $matches)) {\n    print_r($matches); <span class=\"hljs-comment\">\/\/ 30<\/span>\n\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRwYXR0ZXJuID0gJy9cZCsoPz1sYikvJzsKJHN0ciA9ICcyIGNoaWNrZW4gd2VpZ2ggMzBsYic7CgppZiAocHJlZ19tYXRjaCgkcGF0dGVybiwgJHN0ciwgJG1hdGNoZXMpKSB7CiAgICBwcmludF9yKCRtYXRjaGVzKTsgLy8gMzAKCn0\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">Array<\/span>\n(\n    &#91;<span class=\"hljs-number\">0<\/span>] =&gt; <span class=\"hljs-number\">30<\/span>\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='multiple-lookaheads'>Multiple lookaheads <a href=\"#multiple-lookaheads\" class=\"anchor\" id=\"multiple-lookaheads\" title=\"Anchor for Multiple lookaheads\">#<\/a><\/h2>\n\n\n\n<p>The following illustrates the multiple lookaheads:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"> A(?=B)(?=C)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It works like this:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Find A<\/li>\n\n\n\n<li>Test if B is immediately after A, skip if it&#8217;s not.<\/li>\n\n\n\n<li>Test if C is also immediately after B; skip if it&#8217;s not.<\/li>\n\n\n\n<li>If both tests pass, the A is a match; otherwise, search for the next match.<\/li>\n<\/ol>\n\n\n\n<p>In short, the <code>A(?=B)(?=C)<\/code> pattern matches <code>A<\/code> followed by <code>B<\/code> and <code>C<\/code> simultaneously.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='negative-lookahead'>Negative Lookahead <a href=\"#negative-lookahead\" class=\"anchor\" id=\"negative-lookahead\" title=\"Anchor for Negative Lookahead\">#<\/a><\/h2>\n\n\n\n<p>Suppose you want to match only the number <code>2<\/code> in the following text but not the number <code>30<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-number\">2<\/span> chicken weigh <span class=\"hljs-number\">30<\/span>lb<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To do that, you can use the negative lookahead syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">A(?!B)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>A(?!B)<\/code> matches <code>A<\/code> only if followed by <code>B<\/code>. It&#8217;s the <code>\\d+<\/code> not followed by the string <code>lb<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$pattern = <span class=\"hljs-string\">'\/\\d+(?!lb)\/'<\/span>;\n$str = <span class=\"hljs-string\">'2 chicken weigh 30lb'<\/span>;\n\n<span class=\"hljs-keyword\">if<\/span> (preg_match($pattern, $str, $matches)) {\n    print_r($matches); <span class=\"hljs-comment\">\/\/ 2<\/span>\n\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRwYXR0ZXJuID0gJy9cZCsoPyFsYikvJzsKJHN0ciA9ICcyIGNoaWNrZW4gd2VpZ2ggMzBsYic7CgppZiAocHJlZ19tYXRjaCgkcGF0dGVybiwgJHN0ciwgJG1hdGNoZXMpKSB7CiAgICBwcmludF9yKCRtYXRjaGVzKTsgLy8gMgoKfQ\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">Array<\/span>\n(\n    &#91;<span class=\"hljs-number\">0<\/span>] =&gt; <span class=\"hljs-number\">2<\/span>\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the regex lookahead <code>A(?=B)<\/code> that matches <code>A<\/code> only if followed by <code>B<\/code>.<\/li>\n\n\n\n<li>Use the negative regex lookahead <code>A(?!B)<\/code> that matches <code>A<\/code> only if not followed by <code>B<\/code>.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2691\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/regex-lookahead\/\"\n\t\t\t\tdata-post-title=\"Regex Lookahead\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2691\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/regex-lookahead\/\"\n\t\t\t\tdata-post-title=\"Regex Lookahead\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn to use the regex lookahead and negative lookahead by practical examples.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":136,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2691","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2691","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=2691"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2691\/revisions"}],"predecessor-version":[{"id":3263,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2691\/revisions\/3263"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/15"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=2691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}