Reputation: 161
Need to delete brackets and text in it. For now, it almsot working. Deletes all text starting from bracket. My code:
$lines = preg_replace("/\(([^\d]+)/", '', $lines);
For example, text:
Some random words (word1 / word2 / word3) aaaa
Code deletes all text after brackets too,
Some random words
but what I need to do is (what it should look like):
Some random words aaaa
Upvotes: 3
Views: 124
Reputation: 36110
/\([^()]*\)/
\(
- opening bracket[^()]*
- as many characters as possible, which are not brackets\)
- closing bracketYou can optionally remove one space at the end (/\([^()]*\) ?/
) so that you wont have two spaces where the removal happened.
Note that this wont handle cases of nested brackets like:
foo (bar (baz) quiz)
To do that, you will have to be a little more creative:
(?<no_brackets>[^()]*){0}(?<balanced_brackets>\(\g<no_brackets>\)|\(\g<no_brackets>\g<balanced_brackets>\g<no_brackets>\))
Upvotes: 3
Reputation: 52195
You are missing an extra \)
at the end of your expression. As is, your expression will start matching the moment it finds an open bracket and keep on going.
Upvotes: 2