如何在Java中查找字符串模式

时间:2022-02-17 19:24:42

Here is a string like this in Java.

这是Java中的这样一个字符串。

String string="abc$[A]$def$[B]$ghi";

I want to search words that are located in $[*]$ pattern. The result of above the string is A, B.

我想搜索位于$ [*] $ pattern中的单词。字符串上面的结果是A,B。

3 个解决方案

#1


5  

    String s = "abc$[A]$def$[B]$ghi";
    Pattern p = Pattern.compile("\\$\\[.*?\\]\\$");
    Matcher m = p.matcher(s);
    while(m.find()){
        String b =  m.group();
        System.out.println(">> " +b.substring(2, b.length()-2));
    }

#2


0  

Use a regular expression. In Java, you can use the Pattern class.

使用正则表达式。在Java中,您可以使用Pattern类。

#3


0  

You could use regular expressions for that. Take a look at the classes Pattern and Matcher.

你可以使用正则表达式。看看模式和匹配器类。

The regular expression you would use in that case would be:

在这种情况下你将使用的正则表达式是:

\$\[.*?\]\$

Alternatively, you could work with String.indexOf and String.substr.

或者,您可以使用String.indexOf和String.substr。

#1


5  

    String s = "abc$[A]$def$[B]$ghi";
    Pattern p = Pattern.compile("\\$\\[.*?\\]\\$");
    Matcher m = p.matcher(s);
    while(m.find()){
        String b =  m.group();
        System.out.println(">> " +b.substring(2, b.length()-2));
    }

#2


0  

Use a regular expression. In Java, you can use the Pattern class.

使用正则表达式。在Java中,您可以使用Pattern类。

#3


0  

You could use regular expressions for that. Take a look at the classes Pattern and Matcher.

你可以使用正则表达式。看看模式和匹配器类。

The regular expression you would use in that case would be:

在这种情况下你将使用的正则表达式是:

\$\[.*?\]\$

Alternatively, you could work with String.indexOf and String.substr.

或者,您可以使用String.indexOf和String.substr。