Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use regular expressions to remove parentheses

Shulou Source: shulou.com Published: 2022-06-02 11:17:09 09月10日 Update

How do I use regular expressions to remove brackets? In view of this problem, this article introduces the corresponding analysis and answers in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

Example: color: pink [10] size: s [5]

Remove [] and content:

Preg_replace ("/\ [. *\] /",', $str) 1

Effect after treatment: color: pink size: s

Tip: you can change [] to other symbols and apply them where needed.

Ps: let's take a look at extracting the contents in parentheses using regular expressions

For example, now we want to extract the word "China" in parentheses in the sentence "people's Republic of China" for short (China).

Import java.util.regex.Matcher;import java.util.regex.Pattern;public class Test {public static void main (String [] args) {String str = "people's Republic of China, abbreviated as (China)." ; Matcher mat = Pattern.compile ("?

Tags: Regular expression bracket content question China size more Pink Color Republic China help solution easy easy people people examples places Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Docker NVidia Shulou Technology vpn