简体   繁体   中英

Parse a string using Regular Expression in java

I am trying to parse a String using Regular expression. I have a content text:text and I want to parse the content from a string which has text:text. Code:

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);
                }

Ouput:

   from:cal_date_d
type:string
relationship:many_to_one
sql_on:

Expected Output:

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

Try this pattern

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

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

如果你有像"key1:value1 key2:value2..."这样的字符串,那么你可以使用这个正则表达式:

([^ ]*:[^ ]*)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM