?
Solved

parse a string with quote and operators in Java

Posted on 2014-03-28
4
Medium Priority
?
202 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

649 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