This article describes the operation of Java regular extraction of content in brackets. Share it for your reference, as follows:
I once encountered a problem in my work, which was that I needed to extract the content in each bracket in a string. I searched online and found that regular expressions can be used to extract the content in brackets. The specific implementation is as follows:
import java.util.ArrayList;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;public class ExtractMessage { public static void main(String[] args) { String msg = "PerformanceManager[1st bracket]Product[2nd bracket]<[3rd bracket]79~"; List<String> list = extractMessageByRegular(msg); for (int i = 0; i < list.size(); i++) { System.out.println(i+"-->"+list.get(i)); } } /** * Use regular expressions to extract the content in brackets* @param msg * @return */ public static List<String> extractMessageByRegular(String msg){ List<String> list=new ArrayList<String>(); Pattern p = Pattern.compile("(//[[^//]]*//])"); Matcher m = p.matcher(msg); while(m.find()){ list.add(m.group().substring(1, m.group().length()-1)); } return list; }}The output result is as follows:
0-->The 1st bracket
1-->The second bracket
2-->The 3rd bracket
This quickly completed the code work, but later I found that if the brackets still contain brackets, the regular expression will lose its effect. I had to find a way to solve it myself. After research, I finally found a solution. The specific implementation is as follows:
package com.perry.test;import java.util.ArrayList;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;public class ExtractMessage { public static void main(String[] args) { String msg = "PerformanceManager[1st bracket] Product[2nd bracket[2nd bracket contains brackets]]<[3rd bracket]79~"; List<String> list = extractMessage(msg); for (int i = 0; i < list.size(); i++) { System.out.println(i+"-->"+list.get(i)); } } /** * Extract the content in the brackets and ignore the brackets in the brackets* @param msg * @return */ public static List<String> extractMessage(String msg) { List<String> list = new ArrayList<String>(); int start = 0; int startFlag = 0; int endFlag = 0; for (int i = 0; i < msg.length(); i++) { if (msg.charAt(i) == '[') { startFlag++; if (startFlag == endFlag + 1) { start = i; } } else if (msg.charAt(i) == ']') { endFlag++; if (endFlag == startFlag) { list.add(msg.substring(start + 1, i)); } } } return list; }}The output result is as follows:
0-->The 1st bracket
1-->The second bracket [The bracket contains brackets]
2-->The 3rd bracket
The main idea is to traverse the string and mark the beginning and end positions of the brackets. If it is the end position corresponding to the beginning position of the brackets, the counts of the start position and the end position are the same, so the content of the complete brackets is intercepted.
PS: Here are two very convenient regular expression tools for your reference:
JavaScript regular expression online testing tool:
http://tools.VeVB.COM/regex/javascript
Regular expression online generation tool:
http://tools.VeVB.COM/regex/create_reg
For more information about Java algorithms, readers who are interested in this site can view the topics: "Java Regular Expression Skills", "Java Data Structure and Algorithm Tutorial", "Java Operation DOM Node Skills", "Java File and Directory Operation Skills Summary" and "Java Cache Operation Skills Summary"
I hope this article will be helpful to everyone's Java programming.