Reputation: 1395
I have strings which look like this:
"Mo 13:00 - 14:00, Di 15:00-19:00, Montag - Dienstag 18:00-23:00 Montag bis Samstag 03:00 bis 10:00"
and i want to create substrings like this:
"Mo 13:00 - 14:00", "Di 15:00-19:00", "Montag - Dienstag 18:00-23:00"...
if have now created this pattern:
"([a-zA-Z\\s]*\\d\\d[:]\\d\\d\\s\\S*\\s\\d\\d[:]\\d\\d)"
but with Matcher.find() i only get one result:"Mo 13:00 - 14:00"
here is a sample code in case i made a silly mistake:
String xy = "Mo 13:00 - 14:00, Di 15:00-19:00, Montag - Dienstag 18:00-23:00 Montag bis Samstag 03:00 bis 10:00";
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("([a-zA-Z\\s]*\\d\\d[:]\\d\\d\\s\\S*\\s\\d\\d[:]\\d\\d)");
java.util.regex.Matcher matcher = pattern.matcher(xy);
while(matcher.find())
{
System.out.println(matcher.group());
}
Upvotes: 1
Views: 687
Reputation: 48211
Use a regex pattern like this:
([a-zA-Z][a-zA-Z\\s-]*\\d\\d[:]\\d\\d\\s*\\S*?\\s*\\d\\d[:]\\d\\d)`
\______/\___________/\_____________/\__/ \__/\_____________/
| | ##:## |_______| ##:##
| | |
start with accepts any number of account for
a letter letters, spaces or "-" optional spaces
(Required for dates like:
"Montag - Dienstag...")
See, also, this short demo.
You might also want some more info about Regular Expressions.
Upvotes: 4
Reputation: 48404
String dates = "Mo 13:00 - 14:00, Di 15:00-19:00, Montag - Dienstag 18:00-23:00 Montag bis Samstag 03:00 bis 10:00";
Pattern pattern = Pattern.compile("(.+?)((?<=\\d),?\\s(?=[A-Z])|$)");
Matcher matcher = pattern.matcher(dates);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
Output:
Mo 13:00 - 14:00
Di 15:00-19:00
Montag - Dienstag 18:00-23:00
Montag bis Samstag 03:00 bis 10:00
Upvotes: 1
Reputation: 308031
You require whitespace between the times and the dashes: \\s\\S*\\s
. Only the first occurance has such a whitespace. Try making that whitespace optional: \\?\\S*\\s?
.
Upvotes: 1