Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get data between quotes in java?

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

like image 962
atomsfat Avatar asked Sep 24 '09 17:09

atomsfat


People also ask

How do you read a double quoted string in Java?

If you want to print string with double quotes in java using System. out. println, then you can use either \" or '"' with the String.

How do you use quotation marks in Java?

Within a character string, to represent a single quotation mark or apostrophe, use two single quotation marks. (In other words, a single quotation mark is the escape character for a single quotation mark.) A double quotation mark does not need an escape character.

What does 2 quotation marks mean in Java?

For instance, in Java a double-quote is a string while a single-quote is a character. Defining char letter = "a" in Java will cause an error in the same way as String s = 'a bunch of letters' will. Always better to use double-quotes when storing strings. Submitted by Matt.

How do you split a string with double quotes?

Use method String. split() It returns an array of String, splitted by the character you specified.


1 Answers

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.

like image 167
erickson Avatar answered Sep 22 '22 02:09

erickson