Solved

parse a string with quote and operators in Java

Posted on 2014-03-28
4
195 Views
Last Modified: 2014-03-28
Hello ,
  How do I parse this string in java The value in the string is separated by 1 or more space

queryStr= XX -YY "this is test"   
should return
XX
-YY
"this is a test"

Open in new window


queryStr = -XX YY -"this is a test"
Should return
-XX
YY
-"this is a test"

Open in new window


I have this pattern in my java code
 Matcher m = Pattern.compile("([^\"]\\S*|\".+?\")\\s*").matcher(queryStr);

It parses the first string correctly. However, it does not work for the second case (the string in quote with the operator in front"

How can I fix the pattern to make it work in both cases.

Thanks for your help
0
Comment
Question by:SiemensSEN
  • 2
  • 2
4 Comments
 
LVL 13

Expert Comment

by:Carl Bohman
ID: 39961997
This may work:

Matcher m = Pattern.compile("((?:-[^\"\\s]|[^-\"\\s])\\S*|-?\".+?\")\\s*").matcher(queryStr);

Open in new window

0
 

Author Comment

by:SiemensSEN
ID: 39962211
Thank you Carl . It works 90% . However it does not work for this string

queryStr= -"No files to process" +"Posting file "

Thanks for your help
0
 
LVL 13

Accepted Solution

by:
Carl Bohman earned 500 total points
ID: 39962228
I did not know that - and + were both possible based on the original question.  Here is an updated version:

Matcher m = Pattern.compile("((?:[-+][^\"\\s]|[^-+\"\\s])\\S*|[-+]?\".+?\")\\s*").matcher(queryStr);

Open in new window


Note that I added the plus everywhere the dash was included in the regex to ensure consistency.
0
 

Author Closing Comment

by:SiemensSEN
ID: 39962243
Thanks you very much for the help
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hibernate jars 4 58
Better way to make a string with template variables in java 3 33
passing enum to a method 4 35
DTD and JAVA versions 1 29
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question