如何选择以x开头并以y结尾的多重子行的子串并替换它[复制]

问题描述:

This question already has an answer here:

I want to find a regex that selects a specific string that is defined by it's beginning & ending characters, through multilines, to be replaced by empty string. suppose that I have the following string for example:

I want to select every string that begins with -- and ends with ;

$str = 'some text --text_to_be_replaced; and another text -- text
to
be
replaced; and some text...;';

what I've tried:

$str = preg_replace('/--(.*);/s', '', $str);

but the returned result is: some text while the expected result is some text and another text and some text...;

</div>

Your regex is too greedy, try this one:

/--([^;]*);/s

Demo