I am looking for how to separate the text in a MAP in java. For example I have the following text:
2.10 Add nodev Option to Removable Media Partitions (Scored) Profile Description:Set nodev on removable media to prevent character and block special devices that are present , on the removable media from being treated as device files.
So I made the following code using the regex:
String text ="2.10 Add nodev Option to Removable Media Partitions (Scored)"
+"Profile Description:Set nodev on removable media to prevent character and "
+"block special devices that are present"
+", on the removable media from being treated as device files. ";
Map<String, List<String>> maps = new HashMap<>();
Pattern pattern = Pattern.compile("^((\\d+\\.)*?(\\d+)) .*$"); //To find out if there is, for example, 1.1.
Pattern pattern2 = Pattern.compile("[0-9].*?.*[0-9].*$");//To retrieve the title of the paragraph: 1.1. Add Nodev Option to Removable Media Scores
List<String> paragraphe = new ArrayList<>();
maps.put(null, paragraphe);
for(String ligne : text.split("\n")) {
Matcher matcher = pattern.matcher(ligne);
Matcher matcher2 = pattern2.matcher(ligne);
if ( matcher.matches() && matcher2.matches()) {
paragraphe = new ArrayList<>();
maps.put( matcher2.group(0), paragraphe);
paragraphe.add(ligne);
}
else {
paragraphe.add(ligne);
}
}
for (Entry<String, List<String>> key : maps.entrySet()) {
for (String strings : key.getValue()) {
if (strings.contains("(Scored)")) {
System.out.println("Key : " + key.getKey() + " Value : " + key.getValue());
}
}
}
This code displays the following result:
Key : 2.10 Add nodev Option to Removable Media Partitions (Scored)
Value : [2.10 Add nodev Option to Removable Media Partitions (Scored)
Profile Description:Set nodev on removable media to prevent character and block special devices that are present , on the removable media from being treated as device files. ]
But I want to have the following results: the key containing the title (2.10 Add nodev Option to Removable Media Partitions (Scored)) and the value of their content (Profile Description:Set nodev on removable ......):
Key : 2.10 Add nodev Option to Removable Media Partitions (Scored)
Value : [ Profile Description:Set nodev on removable media to prevent character and block special devices that are present , on the removable media from being treated as device files. ]
Someone could help me to get the right result. Thank you
textwith\n, but thetexthas no\n.\n, for example if the line contains the word " (Scored)", so we will only take the contents of this line: 2.10 Add nodev Option to Removable Media (Scored)