Parsing of the string parameterized symbol ${} in java

  • 2020-05-30 20:19:24
  • OfStack

preface

We can see the symbol ${} for parameter meaning in many places, maybe we need to use this symbol sometimes when we write 1 frames, but how exactly do they parse? Or when we need to write it ourselves, how?

Let's start with the following scenarios:

1. String "a${a}a"

2. String "a\${a}a"

3. String "a${a\}a"

4. String "a${a\ a}a"

5. String "a${a}a${"

6. String "a${a}a${a}"

In the above several strings, basically includes the use of 1 some scenarios, so we in the parsing, to consider the various situations, as far as possible to be comprehensive, so that our framework makes sense.

Obviously, we will use regular to parse, so let's create a new JAVA regular class:


public class RegExp {
 
 public boolean match(String reg, String str) {
  return Pattern.matches(reg, str);
 }
 
 public List<String> find(String reg, String str) {
  Matcher matcher = Pattern.compile(reg).matcher(str);
  List<String> list = new ArrayList<String>();
  while (matcher.find()) {
   list.add(matcher.group());
  }
  return list;
 }
  
 public List<String> find(String reg, String str, int index) {
  Matcher matcher = Pattern.compile(reg).matcher(str);
  List<String> list = new ArrayList<String>();
  while (matcher.find()) {
   list.add(matcher.group(index));
  }
  return list;
 }
  
 public String findString(String reg, String str, int index) {
  String returnStr = null;
  List<String> list = this.find(reg, str, index);
  if (list.size() != 0)
   returnStr = list.get(0);
  return returnStr;  
 }
 
 public String findString(String reg, String str) {
  String returnStr = null;
  List<String> list = this.find(reg, str);
  if (list.size() != 0)
   returnStr = list.get(0);
  return returnStr;
 }
 
  
 
 public static void main(String[] args) {
  RegExp re = new RegExp();
  System.out.println(re.find("(a)b", "ababab", 1));
 }
}

Then I began to parse it. It is very simple. One regular is enough:


public class ParseKeyword {
  
 public List<String> getKeywords(String p){
  String reg = "(?<=(?<!\\\\)\\$\\{)(.*?)(?=(?<!\\\\)\\})"; 
  RegExp re = new RegExp();
  List<String> list = re.find(reg, p);
  return list;
 }
  
 public static void main(String[] args) {
  ParseKeyword p = new ParseKeyword();
  System.out.println(p.getKeywords("a${a}a"));
  System.out.println(p.getKeywords("a\\${a}a"));
  System.out.println(p.getKeywords("a${a\\}a"));
  System.out.println(p.getKeywords("a${a\\}a}a"));
  System.out.println(p.getKeywords("a${a}a${"));
  System.out.println(p.getKeywords("a${ab}a${a}"));
 }
}

To parse this parameter symbol, the main thing to master is the regularization, especially in the preview mode (I recommend 1 preview mode article), and then the other is some string operation method.

conclusion

The above is the entire content of this article has been changed, I hope the content of this article can be useful to you, if you have any questions you can leave a message to communicate.


Related articles: