在Java Regex String中查找括号

时间:2021-05-21 21:46:41

I am trying to put all the different types of brackets i.e (, {, and [, in a given string into an array using Regex, but I can't figure out how to do it. Here is my current code :

我试图把所有不同类型的括号,即(,{和[,在给定的字符串中使用正则表达式放入一个数组,但我无法弄清楚如何做到。这是我当前的代码:

String s = "{[()]}";
String regexStr = "\\( | \\) | \\[ | \\] | \\{ | \\}";
Pattern p = Pattern.compile(regexStr);
String[] splitStr = p.split(s);

When I print the array though, there is only one element which is the original string: "{[()]}". I'm not sure if my regexStr is right, I added spaces next to the OR because when I didn't leave spaces, there was nothing being added to the array.

当我打印数组时,只有一个元素是原始字符串:“{[()]}”。我不确定我的regexStr是否正确,我在OR旁边添加了空格,因为当我没有留空格时,没有任何东西被添加到数组中。

The regexStr works in the online Java regex checker I use when I don't have spaces, but it doesn't seem to work in my code.

regexStr在我没有空格时使用的在线Java正则表达式检查器中工作,但它似乎在我的代码中不起作用。

3 个解决方案

#1


1  

You can do something like this:

你可以这样做:

public static void main(String[] args) throws FileNotFoundException {
    ArrayList<String> temp = new ArrayList<String>();
    String s = "{[()]}";
    String regexStr = "[\\{|\\}|\\(|\\)|\\[|\\]]";
    Pattern p = Pattern.compile(regexStr);
    Matcher m = p.matcher(s);
    while(m.find())
    {
        temp.add(m.group());
    }
    //If you want to add results to an array...
    String[] results = new String[temp.size()];
    for(int i = 0; i < temp.size(); i++)
    {
        results[i] = temp.get(i);
    }
    for(String a : results)
    {
        System.out.println(a);
    }
}

#2


1  

you can try converting a string to char array then iterate through that array and if a particular bracket exists, use an arraylist to append

您可以尝试将字符串转换为char数组然后遍历该数组,如果存在特定括号,请使用arraylist追加

string="Hello)"
char[] a= string.toCharArray();
ArrayList brackets= new ArrayList()<>
for(char i : a)
{
if(i==')' || any other bracket)
{
brackets.add(i);
}
}

#3


1  

Why you don't just use "{[()]}".split("") for example :

为什么你不只是使用“{[()]}”。split(“”)例如:

String[] spl = "{[()]}".split("");
System.out.println(Arrays.toString(spl));

Output

[{, [, (, ), ], }]

Solution 2

If your input contain other characters then you can replace them first and use the previouse solution for example :

如果您的输入包含其他字符,那么您可以先替换它们并使用previouse解决方案,例如:

String s = "{[(hello)java]world}";
String[] spl = s.replaceAll("[^\\{\\}\\(\\)\\[\\]]", "").split("");

regex demo

#1


1  

You can do something like this:

你可以这样做:

public static void main(String[] args) throws FileNotFoundException {
    ArrayList<String> temp = new ArrayList<String>();
    String s = "{[()]}";
    String regexStr = "[\\{|\\}|\\(|\\)|\\[|\\]]";
    Pattern p = Pattern.compile(regexStr);
    Matcher m = p.matcher(s);
    while(m.find())
    {
        temp.add(m.group());
    }
    //If you want to add results to an array...
    String[] results = new String[temp.size()];
    for(int i = 0; i < temp.size(); i++)
    {
        results[i] = temp.get(i);
    }
    for(String a : results)
    {
        System.out.println(a);
    }
}

#2


1  

you can try converting a string to char array then iterate through that array and if a particular bracket exists, use an arraylist to append

您可以尝试将字符串转换为char数组然后遍历该数组,如果存在特定括号,请使用arraylist追加

string="Hello)"
char[] a= string.toCharArray();
ArrayList brackets= new ArrayList()<>
for(char i : a)
{
if(i==')' || any other bracket)
{
brackets.add(i);
}
}

#3


1  

Why you don't just use "{[()]}".split("") for example :

为什么你不只是使用“{[()]}”。split(“”)例如:

String[] spl = "{[()]}".split("");
System.out.println(Arrays.toString(spl));

Output

[{, [, (, ), ], }]

Solution 2

If your input contain other characters then you can replace them first and use the previouse solution for example :

如果您的输入包含其他字符,那么您可以先替换它们并使用previouse解决方案,例如:

String s = "{[(hello)java]world}";
String[] spl = s.replaceAll("[^\\{\\}\\(\\)\\[\\]]", "").split("");

regex demo