?
Solved

parse a string with quote and operators in Java

Posted on 2014-03-28
4
Medium Priority
?
198 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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…
Suggested Courses
Course of the Month11 days, 10 hours left to enroll

752 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