3

I have the following code to extract the string within double quotes using Regex.

String str ="\"Java\",\"programming\"";
final Pattern pattern = Pattern.compile("\"([^\"]*)\"");
final Matcher matcher = pattern.matcher(str);
while(matcher.find()){
    System.out.println(matcher.group(1));
}

The output I get now is java programming.But from the String str I want the content in the second double quotes which is programming. Can any one tell me how to do that using Regex.

0

3 Answers 3

4

If you take your example, and change it slightly to:

String str ="\"Java\",\"programming\"";
final Pattern pattern = Pattern.compile("\"([^\"]*)\"");
final Matcher matcher = pattern.matcher(str);
int i = 0
while(matcher.find()){
    System.out.println("match " + ++i + ": " + matcher.group(1) + "\n");
}

You should find that it prints:

match 1: Java
match 2: programming

This shows that you are able to loop over all of the matches. If you only want the last match, then you have a number of options:

  • Store the match in the loop, and when the loop is finished, you have the last match.
  • Change the regex to ignore everything until your pattern, with something like: Pattern.compile(".*\"([^\"]*)\"")

If you really want explicitly the second match, then the simplest solution is something like Pattern.compile("\"([^\"]*)\"[^\"]*\"([^\"]*)\""). This gives two matching groups.

Sign up to request clarification or add additional context in comments.

Comments

2

If you want the last token inside double quotes, add an end-of-line archor ($):

final Pattern pattern = Pattern.compile("\"([^\"]*)\"$");

In this case, you can replace while with if if your input is a single line.

Comments

1

Great answer from Paul. Well,You can also try this pattern

final Pattern pattern = Pattern.compile(",\"(\\w+)\"");

Java program

     String str ="\"Java\",\"programming\"";
final Pattern pattern = Pattern.compile(",\"(\\w+)\"");
final Matcher matcher = pattern.matcher(str);
while(matcher.find()){
    System.out.println(matcher.group(1));
}

Explanation

,\": matches a comma, followed by a quotation mark "

(\\w+): matches one or more words

\": matches the last quotation mark "

Then the group(\\w+) is captured (group 1 precisely)

Output

programming

5 Comments

What if I want to print Java and Programming separate using 2 different print statement. How can I do that?
then use this pattern final Pattern pattern = Pattern.compile("([^\",]+)");
I want to separate java and programming into two different strings and print it.
ok. its now i understand what you meant. Use this final Pattern pattern = Pattern.compile("\"(\\w+)\",\"(\\w+)\""); then System.out.print(matcher.group(1)), System.out.print(matcher.group(2)).
Thanks for your suggestion.

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.