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
  • 1
    You need to learn how to READ and debug error messages. Everything you needed to solve the problem was in the message. stackoverflow.com/questions/12769982/… Commented Jul 22, 2013 at 11:38

4 Answers 4

3

You have missed semicolon ; at the first line

2

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

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! :)

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";
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.