0

I wanted to perform a regex in php. From the $str variable I want to select out common_name = Bangladesh . I wore my regex. The regex is not finding any match here. How can I fix this?

$str = "| conventional_long_name = People's Republic of Bangladesh | common_name = Bangladesh | image_flag = Flag of Bangladesh.svg";
    $pat = "/(?=\common_name\s=).*?(?=\s\|)/";

    if(preg_match($pat, $str, $matches)){
        echo "Matches found";
        echo $matches[0];
    } else {
        echo "No match found";
    }

the result shows

No match found
  • 1
    You need to learn how to READ and debug error messages. Everything you needed to solve the problem was in the message. http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php – Anigel Jul 22 '13 at 11:38

4 Answers4

3

You have missed semicolon ; at the first line

Molecular Man
  • 22,277
  • 3
  • 72
  • 89
2

You need to put ; at the end of $str = "...."

DeiForm
  • 701
  • 9
  • 31
0

You're missing a semicolon at the end of your first line:

Change it to:

$str = "| conventional_long_name = People's Republic of Bangladesh | common_name = Bangladesh | image_flag = Flag of Bangladesh.svg";

Also, get a decent IDE and save yourself the trouble of asking this question on SO every time! :)

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
0

You are probably missing the semicolon on the 1st line of your code

  $str = "| conventional_long_name = People's Republic of Bangladesh | common_name = Bangladesh | image_flag = Flag of Bangladesh.svg";
        $pat = "/(?=\common_name\s=).*?(?=\s\|)/";

        if(preg_match($pat, $str, $matches)){
            echo "Matches found";
            echo $matches[0];
        } else {
            echo "No match found";
        }
Chetan Potdar
  • 827
  • 1
  • 7
  • 12