Reputation: 4289
I have the following string(contains Portuguese characters) in the following structure: contain Name:
and then some words after.
Example:
String myStr1 = "aaad Name: bla and more blá\n gdgdf ppp";
String myStr2 = "bbbb Name: Á different blÁblÁ\n hhhh fjjj";
I need to extract the string from 'Name:'
till the end of the line.
example:
extract(myStr1) = "Name: bla and more blá"
extract(myStr2) = "Name: Á different blÁblÁ"
Edit after @blue_note answer:
here is what I tried:
public static String extract(String myStr) {
Pattern p = compile("Name:(?m)^.*$");
Matcher m = p.matcher(myStr);
while (m.find()) {
String theGroup = m.group(0);
System.out.format("'%s'\n", theGroup);
return m.group(0);
}
return null;
}
did not work.
Upvotes: 2
Views: 9554
Reputation: 29099
The regex is "^\\w*\\s*((?m)Name.*$)")
where
?m
enables the multiline mode^, $
denote start of line and end of line respectively.*
means any character, any number of timesAnd get group(1)
, not group(0)
of the matched expression
Upvotes: 4
Reputation: 916
You could also use substring in this case:
String name = myStr1.substring(myStr1.indexOf("Name:"), myStr1.indexOf("\n"));
Upvotes: 1