Reputation: 327
I am looking for Regex Pattern extraction to extract the following example ,
sample Input:
"USER 1-EFA-Thermal Analysis-0.25;USER 2-EFA-Thermal Analysis-0.25;"
String[] parts = filename.split("\\;");
for (int i=0;i<=parts.length;i++){
}
The above code split the line into Multiple part with ";" as delimiter. Inside the For Loop i want to again the split each part[] into three phrases which is identified by "-" (dash).But problem being the Dash can also appear within the field to be separated .
So my logic to is to split the Words Before First occurrence of "-" as 1st Part.
String After First Occurrence of "dash" till last occurrence of "dash" as 2nd And the remaining being 3rd Part
For example :
part[0]
USER 1
EFA-Thermal Analysis
0.25
part[1]
USER 2
EFA-Thermal Analysis
0.25
Upvotes: 1
Views: 107
Reputation: 771
Use a regex to match everything until the first "minus":
part.replaceAll("^([^\\-]*)-", "$1\n")
and everything from the last minus until the end of the string:
part.replaceAll("-([^\\-]*?)$", "\n$1")
Code
String sample = "USER 1-EFA-Thermal Analysis-0.25;USER 2-EFA-Thermal Analysis-0.25;";
String[] parts = sample.split("\\;");
for (String part : parts) {
part = part.replaceAll("^([^\\-]*)-", "$1\n");
part = part.replaceAll("-([^\\-]*?)$", "\n$1");
System.out.println(part);
}
Output
USER 1
EFA-Thermal Analysis
0.25
USER 2
EFA-Thermal Analysis
0.25
Upvotes: 2
Reputation: 52185
The .split(string regex)
method will most likely not work, at least I cannot see how it can be used to achieve a relatively simple solution. The problem, in my opinion is that the dash can appear in between different character types which makes it tricky to latch onto.
I managed to achieve what you are after through a slightly different usage of regular expressions:
String str = "USER 1-EFA-Thermal Analysis-0.25;USER 2-EFA-Thermal Analysis-0.25;";
Pattern pattern = Pattern.compile("(.+?)-(.+?)-(\\d+(\\.\\d+)?);");
Matcher matcher = pattern.matcher(str);
while(matcher.find())
System.out.println(matcher.group(1) + " " + matcher.group(2) + " " + matcher.group(3));
The code below yields the following information:
USER 1 EFA-Thermal Analysis 0.25
USER 2 EFA-Thermal Analysis 0.25
Which, according to the samples you have provided, should do what you are after. An example of the regular expression is available here.
Upvotes: 3