java中字符串參數(shù)化符號(hào)${}的解析
前言
我們?cè)诤芏嗟胤蕉寄芸吹酱韰?shù)意義的符號(hào)${},可能我們?cè)趯懸恍┛蚣艿臅r(shí)候,有時(shí)候也需要用到這個(gè)符號(hào),但他們是如何精確解析的?或者說需要我們自已寫的時(shí)候,如何寫?
我們先來看以下的幾個(gè)場(chǎng)景:
1.字符串"a${a}a"
2.字符串"a\${a}a"
3.字符串"a${a\}a"
4.字符串"a${a\}a}a"
5.字符串"a${a}a${"
6.字符串"a${a}a${a}"
以上幾個(gè)字符串中,基本上包括了使用的一些場(chǎng)景,所以我們?cè)诮馕龅臅r(shí)候,要把各種情況都考慮清楚,盡量的做到全面,這樣我們的框架才有意義。
很顯然,我們都會(huì)采用正則來解析,于是我們來新建一個(gè)JAVA正則的類:
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));
}
}
然后開始來解析了,很簡(jiǎn)單,一個(gè)正則即可:
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}"));
}
}
解析這個(gè)參數(shù)符號(hào),要掌握的主要是正則,其中尤其以預(yù)查模式(推薦一篇預(yù)查模式的文章),然后其它的就是一些字符串的操作方法了。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容改了,希望本文的內(nèi)容能對(duì)大家有用,如果有疑問大家可以留言交流。
相關(guān)文章
Java8 Optional判空詳解(簡(jiǎn)化判空操作)
這篇文章主要給大家介紹了關(guān)于Java8 Optional判空(簡(jiǎn)化判空操作)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
java實(shí)現(xiàn)人工智能化屏幕監(jiān)控窗口
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)人工智能化屏幕監(jiān)控窗口,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
springboot+springsecurity如何實(shí)現(xiàn)動(dòng)態(tài)url細(xì)粒度權(quán)限認(rèn)證
這篇文章主要介紹了springboot+springsecurity如何實(shí)現(xiàn)動(dòng)態(tài)url細(xì)粒度權(quán)限認(rèn)證的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
mybatis-plus團(tuán)隊(duì)新作mybatis-mate實(shí)現(xiàn)數(shù)據(jù)權(quán)限
本文主要介紹了mybatis-plus 團(tuán)隊(duì)新作 mybatis-mate 輕松搞定數(shù)據(jù)權(quán)限,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
使用redisTemplate的scan方式刪除批量key問題
這篇文章主要介紹了使用redisTemplate的scan方式刪除批量key問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
Java輕松使用工具類實(shí)現(xiàn)獲取MP3音頻時(shí)長(zhǎng)
在Java中,工具類定義了一組公共方法,這篇文章將介紹Java中使用工具類來獲取一個(gè)MP3音頻文件的時(shí)間長(zhǎng)度,感興趣的同學(xué)繼續(xù)往下閱讀吧2021-10-10
使用Jenkins來構(gòu)建SVN+Maven項(xiàng)目的實(shí)現(xiàn)
這篇文章主要介紹了使用Jenkins來構(gòu)建SVN+Maven項(xiàng)目的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09

