phpregextext-extraction

Get line after a newline with regex


I can't select multiple lines between two characters on regex. How can I solve this?

{
example
example 1
}

I want to select example. but I cant.

I tried this regex

#\n.*#

Solution

  • Your pattern does not do what you expect; it matches a newline character followed by any character except newline "zero or more" times. You need to use the s (dotall) modifier which forces the dot to match across newline sequences.

    For example — matching everything between the two curly braces.

    preg_match('/{(.*)}/s', $str, $match);
    echo $match[1];