Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
747 views
in Technique[技术] by (71.8m points)

perl - Regex: delete contents of square brackets

Is there a regular expression that can be used with search/replace to delete everything occurring within square brackets (and the brackets)?

I've tried [.*] which chomps extra stuff (e.g. "[chomps] extra [stuff]")

Also, the same thing with lazy matching [.*?] doesn't work when there is a nested bracket (e.g. "stops [chomping [too] early]!")

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try something like this:

$text = "stop [chomping [too] early] here!";
$text =~ s/[([^[]]|(?0))*]//g;
print($text);

which will print:

stop  here!

A short explanation:

[            # match '['
(             # start group 1
  [^[]]     #   match any char except '[' and ']'
  |           #   OR
  (?0)        #   recursively match group 0 (the entire pattern!)
)*            # end group 1 and repeat it zero or more times
]             # match ']'

The regex above will get replaced with an empty string.

You can test it online: http://ideone.com/tps8t

EDIT

As @ridgerunner mentioned, you can make the regex more efficiently by making the * and the character class [^[]] match once or more and make it possessive, and even by making a non capturing group from group 1:

[(?:[^[]]++|(?0))*+]

But a real improvement in speed might only be noticeable when working with large strings (you can test it, of course!).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...