Reputation: 4140
I have a string template that looks something like this:
This position is reserved for <XXXXXXXXXXXXXXXXXXXXXXXXXXX>. Start date is <XXXXXXXX>
Filled out, this might look like this (fixed width is preserved):
This position is reserved for <JOHN SMITH >. Start date is <20150228>
How can I extract multiple differences in a single String? I don't want to use an entire templating engine for one task if I can avoid it.
Upvotes: 0
Views: 99
Reputation: 22963
If the template might be modified you could use a format pattern.
String expected = "This position is reserved for <JOHN SMITH >. Start date is <20150228>";
System.out.println(expected);
// define the output format
String template = "This position is reserved for <%-27s>. Start date is <%s>";
String name = "JOHN SMITH";
String startDate = "20150228";
// output the values using the defined format
System.out.println(String.format(template, name, startDate));
Upvotes: 1
Reputation: 36304
You can try regex like this :
public static void main(String[] args) {
String s = "This position is reserved for <JOHN SMITH >. Start date is <20150228>";
Pattern p = Pattern.compile(".*?<(.*?)>.*<(.*?)>");
Matcher m = p.matcher(s);
while(m.find()){
System.out.println("Name : " +m.group(1).trim());
System.out.println("Date : " +m.group(2).trim());
}
}
O/P :
Name : JOHN SMITH
Date : 20150228
Upvotes: 10