how to get data between quotes in java?
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.
How to extract string between double quotes in java?
This code will replace all quotes with one space and save quotes without brackets in the list:
public static void main(String[] args) {
String str = "According to some, dreams express \"profound aspects of personality\" (Foulkes 184), though \"others disagree\" but truth is.";
Pattern pattern = Pattern.compile("\".*?\"");
Matcher matcher = pattern.matcher(str);
List<String> quotes = new ArrayList<>();
StringBuffer buffer = new StringBuffer();
while (matcher.find()) {
String quote = matcher.group();
int length = quote.length();
quotes.add(quote.substring(1, length - 1));
matcher.appendReplacement(buffer, " ");
}
matcher.appendTail(buffer);
System.out.println(buffer.toString());
System.out.println(quotes);
}
This solution needs some additional fixes depends on existence of empty brackets in a text, but it work in your case.
Output:
According to some, dreams express (Foulkes 184), though but truth is.
[profound aspects of personality, others disagree]
Getting data between single and double quotes (special case)
I would try without capture quote type/lookahead/backref to improve performance. See this question for escaped characters in quoted strings. It contains a nice answer that is unrolled. Try like
'[^\\']*(?:\\.[^\\']*)*'|"[^\\"]*(?:\\.[^\\"]*)*"
As a Java String:
String regex = "'[^\\\\']*(?:\\\\.[^\\\\']*)*'|\"[^\\\\\"]*(?:\\\\.[^\\\\\"]*)*\"";
The left side handles single quoted, the right double quoted strings. If either kind overbalances the other in your source, put that preferably on the left side of the pipe.
See this a demo at regex101 (if you need to capture what's inside the quotes, use groups)
How to get the string between double quotes in a string in Java
An alternative using a messy regular expression:
public static void main(String[] args) throws Exception {
Pattern p = Pattern.compile("^(\\w*)[\\s]+(\\w*)[\\s]+(\\w*)[\\s]+(\\w*)[\\s]+(\\w*)[\\s]+[“](.*)[”][\\s]+[“](.*)[”][\\s]+[“](.*)[”]");
Matcher m = p.matcher("AddItem rt456 4 12 BOOK “File Structures” “Addison-Wesley” “Michael Folk”");
if (m.find()) {
for (int i=1;i<=m.groupCount();i++) {
System.out.println(m.group(i));
}
}
}
That prints:
AddItem
rt456
4
12
BOOK
File Structures
Addison-Wesley
Michael Folk
I assumed quotes are as you typed them in the question “” and not "", so they dont need to be escaped.
RegEx: Grabbing values between quotation marks
I've been using the following with great success:
(["'])(?:(?=(\\?))\2.)*?\1
It supports nested quotes as well.
For those who want a deeper explanation of how this works, here's an explanation from user ephemient:
([""'])
match a quote;((?=(\\?))\2.)
if backslash exists, gobble it, and whether or not that happens, match a character;*?
match many times (non-greedily, as to not eat the closing quote);\1
match the same quote that was use for opening.
How to get data between quotes that isn't surrounded by parentheses
This regex should find the values you're looking for:
[^(]\".*\"
Test it here: https://regex101.com/r/oCT1V1/1
Related Topics
How to Create a Topic in Kafka Through Java
How to Create Sparksession with Hive Support (Fails with "Hive Classes Are Not Found")
Differencebetween @Inject and @Autowired in Spring Framework? Which One to Use Under What Condition
Convert from List<Completablefuture> to Completablefuture<List>
How to Get the Caller Class in Java
How to Print Formatted Bigdecimal Values
Java.Lang.Illegalaccesserror: Tried to Access Method
What Is the Cross-Platform Way of Obtaining the Path to the Local Application Data Directory
Why Business Logic Should Be Moved Out of Jsp
Executors.Newcachedthreadpool() Versus Executors.Newfixedthreadpool()
How to Update Path Variable Permanently from Windows Command Line
Java's L Number (Long) Specification
Streaming Large Files in a Java Servlet
Get the Index of a Pattern in a String Using Regex
How to Decode JSON with Unknown Field Using Gson