0

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)?

1
  • A great site to develop RegEx is regex101.com Commented Feb 25, 2016 at 19:41

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.