簡體   English   中英

在java中使用正則表達式解析字符串

[英]Parse a string using Regular Expression in java

我正在嘗試使用正則表達式解析字符串。 我有一個內容 text:text,我想從一個包含 text:text 的字符串中解析內容。 代碼:

String lines=" from:cal_date_d type:string relationship:many_to_one sql_on:${fact_customer.dw_update_date} = ${cal_date_d.dw_update_date}";
Pattern p = Pattern.compile("(\"?[\\w ]*)\\:(\"?([\\w]*)\"?)");
                Matcher m = p.matcher(lines);
                while(m.find()) {
                    String Column_Data=m.group(0);
                    System.out.println("Regex:           "+Column_Data);
                }

輸出:

   from:cal_date_d
type:string
relationship:many_to_one
sql_on:

預期輸出:

from:cal_date_d 
type:string 
relationship:many_to_one 
sql_on:${fact_customer.dw_update_date} = ${cal_date_d.dw_update_date}

試試這個模式

([^\s]+( ?= ?[^\s]*)?)

https://regex101.com/r/c0q4W0/2

如果你有像"key1:value1 key2:value2..."這樣的字符串,那么你可以使用這個正則表達式:

([^ ]*:[^ ]*)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM