Solved

parse a string with quote and operators in Java

Posted on 2014-03-28
4
197 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

717 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