bharani
bharani

Reputation: 81

Extracting a string using Regex

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.

Upvotes: 3

Views: 83

Answers (3)

james jelo4kul
james jelo4kul

Reputation: 829

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

Upvotes: 1

Paul Wagland
Paul Wagland

Reputation: 29116

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.

Upvotes: 4

M A
M A

Reputation: 72844

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.

Upvotes: 2

Related Questions