简体   繁体   中英

Regex to remove [] and everything in them

I am having some troubles getting some regex to work to remove some text, I need [text] removing from [text][maybehere]anytext .

So basically the first set of [] and whatever is in them. The rest of the text can contain anything including spaces and may have another set of [].

I have this: /\[.*\]/ but it is removing all of the brakets and contents.

I am using PHP

Any ideas?

Note the 4th parameter (replace limit) in preg_replace :

$str = preg_replace('/\[[^\]]*\]/', '', $str, 1);

I'd suggest

preg_replace('#\[.*?\]#', '', $stringVar, 1);

It will remove the first matching [] including it's content completely.

How about:

"/^\[[^\]]*\](.*)$/"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM