Regex Match between special character -
i have string similar this:
++ name1 description:asdfmkdfkmfkskfsaf \n ++ name2 description:asdfmkdfkmfkskfsaf \n ++ name3 description:asdfmkdfkmfkskfsaf
i want extract groups like:
group1: name1 description:asdfmkdfkmfkskfsaf group2: name2 description:asdfmkdfkmfkskfsaf group3: name3 description:asdfmkdfkmfkskfsaf
how regex?
maybe thats u like?
public static void main(string[] args) throws malformedurlexception, ioexception { string text = "++ name1\n" + "description:asdfmkdfkmfkskfsaf \\n\n" + "++ name2\n" + "description:asdfmkdfkmfkskfsaf \\n\n" + "++ name3 description:asdfmkdfkmfkskfsaf\n" + "++ name4 :asdfmkdfkmfkskfsaf\n" + "++ name5 asdfmkdfkmfkskfsaf\n"; system.out.println(text); system.out.println("\n\n\nresult:"); matcher matcher = pattern.compile("\\+\\+ (.*?)\\s+(?:description)?:?([\\p{alpha}]+)\\s*").matcher(text); // ^ ^ `- description "could" there. // | `- read whitespaces , linebreaks // \ 2 ++ , whitespace // while(matcher.find()) { system.out.println(matcher.group(1) + " - " + matcher.group(2)); } }
it print:
result: name1 - asdfmkdfkmfkskfsaf name2 - asdfmkdfkmfkskfsaf name3 - asdfmkdfkmfkskfsaf name4 - asdfmkdfkmfkskfsaf name5 - asdfmkdfkmfkskfsaf
Comments
Post a Comment