Consider the following string:
<spring:message code="common.about.team1" /> <br /> <br /> <spring:message code="common.about.team2" /> <br /> <br /> <spring:message code="common.about.team3" /></p>
What would be the corresponding Regular Expression that results in substrings in double quotes, that is: common.about.team1, common.about.team2, common.about.team3 (preferably in Java, but not required)?
-
A great site to develop RegEx is regex101.comJMichaelTX– JMichaelTX2016-02-25 19:41:04 +00:00Commented Feb 25, 2016 at 19:41
Add a comment
|
2 Answers
You would be good looking into Pattern matching. However, I have included a snippet below that may help you solve your issue.
final static String str = "<spring:message code=\"common.about.team1\" />";
final static Pattern pattern = Pattern.compile( "\"(.+?)\"" );
final static Matcher matcher = pattern.matcher( str );
public static void main( String[] args )
{
while ( matcher.find() )
{
System.out.println( matcher.group( 1 ) );
}
}
A good thing for the future would be to look at the following site and attempt to create your regex string: http://regexr.com/3cse4
When you have your regex string working you can then look at implementing it and getting it to work with your application.
Output:
common.about.team1
common.about.team2
common.about.team3
Comments
The following is another pattern which should work:
public static void main(String[] args) {
String input = "<spring:message code=\"common.about.team1\" /> <br /> <br /> <spring:message code=\"common.about.team2\" /> <br /> <br /> <spring:message code=\"common.about.team3\" /></p>";
Pattern p = Pattern.compile("\"([^\"]+)\"");
Matcher m = p.matcher(input);
List<String> matches= new ArrayList<String>();
while (m.find()) {
matches.add(m.group());
}
System.out.println(matches);
}
Output:
["common.about.team1", "common.about.team2", "common.about.team3"]