java中字符串參數(shù)化符號(hào)${}的解析
前言
我們?cè)诤芏嗟胤蕉寄芸吹酱韰?shù)意義的符號(hào)${},可能我們?cè)趯?xiě)一些框架的時(shí)候,有時(shí)候也需要用到這個(gè)符號(hào),但他們是如何精確解析的?或者說(shuō)需要我們自已寫(xiě)的時(shí)候,如何寫(xiě)?
我們先來(lái)看以下的幾個(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ì)采用正則來(lái)解析,于是我們來(lái)新建一個(gè)JAVA正則的類(lèi):
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)); } }
然后開(kāi)始來(lái)解析了,很簡(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ì)大家有用,如果有疑問(wèn)大家可以留言交流。
相關(guān)文章
Java編寫(xiě)網(wǎng)上超市購(gòu)物結(jié)算功能程序
這篇文章主要為大家詳細(xì)介紹了Java編寫(xiě)網(wǎng)上超市購(gòu)物結(jié)算功能程序的具體代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06SpringBoot自定義FailureAnalyzer詳解
這篇文章主要介紹了SpringBoot自定義FailureAnalyzer詳解,FailureAnalyzer是一種在啟動(dòng)時(shí)攔截?exception?并將其轉(zhuǎn)換為?human-readable?消息的好方法,包含在故障分析中,需要的朋友可以參考下2023-11-11Java經(jīng)典排序算法之冒泡排序代碼實(shí)例
這篇文章主要介紹了Java經(jīng)典排序算法之冒泡排序代碼實(shí)例,相鄰兩元素進(jìn)行比較,如過(guò)左側(cè)元素大于右側(cè)元素,則進(jìn)行交換,每完成一次循環(huán)就將最大元素排在最后,下一次循環(huán)是將其它的數(shù)進(jìn)行類(lèi)似操作,需要的朋友可以參考下2023-11-11通過(guò)@Resource注解實(shí)現(xiàn)屬性裝配代碼詳解
這篇文章主要介紹了通過(guò)@Resource注解實(shí)現(xiàn)屬性裝配代碼詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01Java為什么基本數(shù)據(jù)類(lèi)型不需要進(jìn)行創(chuàng)建對(duì)象?
今天小編就為大家分享一篇關(guān)于Java為什么基本數(shù)據(jù)類(lèi)型不需要進(jìn)行創(chuàng)建對(duì)象?,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-04-04screw?Maven插件方式運(yùn)行時(shí)在編譯打包時(shí)跳過(guò)執(zhí)行的問(wèn)題解決方法
這篇文章主要介紹了screw?Maven插件方式運(yùn)行時(shí)在編譯打包時(shí)跳過(guò)執(zhí)行的問(wèn)題解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03