31

I have this lines of text the number of quotes could change like:

Here just one "comillas"
But I also could have more "mas" values in "comillas" and that "is" the "trick"
I was thinking in a method that return "a" list of "words" that "are" between "comillas"

How I obtain the data between the quotes?

The result should be:

comillas
mas, comillas, trick
a, words, are, comillas

vefthym
  • 7,422
  • 6
  • 32
  • 58
atomsfat
  • 2,863
  • 6
  • 34
  • 36

6 Answers6

63

You can use a regular expression to fish out this sort of information.

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

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.

erickson
  • 265,237
  • 58
  • 395
  • 493
  • Sorry, I was missing a double quote there, myself! – erickson Sep 24 '09 at 18:04
  • Can you explain what does `([^\"]*)` do? – user1071840 Jun 19 '18 at 14:57
  • 4
    @user1071840 The enclosing parentheses `( )` make this a *capturing* group: the content matched can be referenced later. The square brackets `[ ]` define a character class: any character inside will be matched---but the `^` negates the class: any character *except* those listed will be matched. The `\"` is a double-quote, escaped because double-quote is the Java string literal delimiter. The `*` means match zero or more of the preceding pattern, possessively. So, all together, `([^\"]*)` means, "Match zero or more of any characters except double quote, and remember them as a group." – erickson Jun 19 '18 at 16:23
20

Check out StringUtils in the Apache commons-lang library - it has a substringsBetween method.

String lineOfText = "if(getip(document.referrer)==\"www.eg.com\" || getip(document.referrer)==\"192.57.42.11\"";

String[] valuesInQuotes = StringUtils.substringsBetween(lineOfText , "\"", "\"");

assertThat(valuesInQuotes[0], is("www.eg.com"));
assertThat(valuesInQuotes[1], is("192.57.42.11"));
Renaud
  • 16,073
  • 6
  • 81
  • 79
SingleShot
  • 18,821
  • 13
  • 71
  • 101
2
String line = "if(getip(document.referrer)==\"www.eg.com\" || getip(document.referrer)==\"192.57.42.11\"";
StringTokenizer stk = new StringTokenizer(line, "\"");
stk.nextToken();
String egStr = stk.nextToken();
stk.nextToken();
String ipStr = stk.nextToken();
jsight
  • 27,819
  • 25
  • 107
  • 140
1

Firstly, please note that you should user equals() rather than ==. "==" by default asks if they are the same instance in memory, which in Strings can sometimes be the case. With myString.equals("...") you're comparing the values of the Strings.

As for how do you obtain the values between the quotes I'm not sure what you mean. "..." is an actual object. Alternatively you could do:

String webUrl = "www.eg.com";

Stephane Grenier
  • 15,527
  • 38
  • 117
  • 192
1

If you are parsing an entire source file rather than just one line, a parser based on the grammar of the function might be a safer choice than trying to do this based on strings.

I am guessing that these would be string literals in your grammar.

Uri
  • 88,451
  • 51
  • 221
  • 321
1

If you want to get all ocurrences from a file:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class testReadQuotes {


    public static void main(String args[]) throws IOException{

        Pattern patt = Pattern.compile("\"([^\"]*)\"");
        BufferedReader r = new BufferedReader(new FileReader("src\\files\\myFile.txt"));

        String line;

        while ((line = r.readLine()) != null) {

          Matcher m = patt.matcher(line);

          while (m.find()) {
            System.out.println(m.group(0));
          }

        }

    }

}
Dani
  • 1,825
  • 2
  • 15
  • 29