How to find a string pattern in Java

Here is a line in Java.

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

I want to find the words that are in the pattern $[*]$ . The result above the line: A , B

+4
source share
3 answers
  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)); } 
+3
source

Use regex. In Java, you can use a template class .

0
source

You can use regular expressions for this. Take a look at the Pattern and Matcher classes.

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

 \$\[.*?\]\$ 

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

0
source

Source: https://habr.com/ru/post/1436762/


All Articles