Skip to main content

APIzation: Replication Package

[Q#1473155][A#1473198] how to get data between quotes in java?

https://stackoverflow.com/q/1473155

I have this lines of text the number of quotes could change like: how I obtain the data between the quotes the result should be?: comillas mas, comillas, trick a, words, are, comillas

Answer

https://stackoverflow.com/a/1473198

You can use a regular expression to fish out this sort of information. This example assumes that the language of the line being parsed doesn't support escape sequences for double-quotes within string literals, contain strings that span multiple "lines", or support other delimiters for strings like a single-quote.

APIzation

Pattern p = Pattern.compile("\"([^\"]*)\"");
Matcher m = p.matcher(line);
while (m.find()) {
  System.out.println(m.group(1));
}
package com.stackoverflow.api;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Human1473198 {

  public static void printDataBetweenQuotes(String line) {
    Pattern p = Pattern.compile("\"([^\"]*)\"");
    Matcher m = p.matcher(line);
    while (m.find()) {
      System.out.println(m.group(1));
    }
  }
}

package com.stackoverflow.api;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * how to get data between quotes in java?
 *
 * @author APIzator
 * @see <a href="https://stackoverflow.com/a/1473198">https://stackoverflow.com/a/1473198</a>
 */
public class APIzator1473198 {

  public static void getDatum(String line) throws Exception {
    Pattern p = Pattern.compile("\"([^\"]*)\"");
    Matcher m = p.matcher(line);
    while (m.find()) {
      System.out.println(m.group(1));
    }
  }
}