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