aspose file tools*
The moose likes Java in General and the fly likes problem in writing Regular Expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "problem in writing Regular Expression" Watch "problem in writing Regular Expression" New topic
Author

problem in writing Regular Expression

Dawood Mohammed
Greenhorn

Joined: Sep 06, 2005
Posts: 7
Hi,
I have a string from which i want to get strings seperated by space, which may also include string with double quotes.

for e.g: business political international "sports news" "local news" editorial

The result of parsing the above string with regular expression must be:
business
political
sports news
local news
editorial

please help me in solving this problem, this is pretty urgent.

Thanks and regards,
Dawood.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

What have you tried so far?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Dawood Mohammed
Greenhorn

Joined: Sep 06, 2005
Posts: 7
I have tried using lookahead, lookbehind regular expressions to solve this problem. but, couldnt work out how lookahead and lookbehind works
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
Originally posted by Dawood Mohammed:
please help me in solving this problem, this is pretty urgent.


Is it as urgent as your other problem ? I just need to know which to concentrate all my efforts on first.

Ease Up


Joanne
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Neither of your two "regular expressions" problems are really very well-suited to regular expressions. They're really both simple state-machine type parsing problems, both simple enough to solve with an ad-hoc lexical analyzer, but each non-trivial enough to justify using something like ANTLR instead if you wanted to.


[Jess in Action][AskingGoodQuestions]
Dawood Mohammed
Greenhorn

Joined: Sep 06, 2005
Posts: 7
thanx for ur quick response. actually both the problems are very urgent for me as both of them are interrelated in my project.
Dawood Mohammed
Greenhorn

Joined: Sep 06, 2005
Posts: 7
Friedman,
Thanx for ur response. how do we use antlr or state machines in java program can u please give an example as I dont have any idea about antlr.

Thanx in advance
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Here is a tutorial on doing a very simple parsing problem with ANTLR.
Kevin Davies
Greenhorn

Joined: Jun 20, 2005
Posts: 14
This is an easy regex problem, don't waste your time trying to write your own lexer :
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Nice. I stand corrected.
Dawood Mohammed
Greenhorn

Joined: Sep 06, 2005
Posts: 7
Thanx a lot for ur solution Kevin Davies.

can u please give me the solution for my other question:

For the example below, can I write a regular expression to get key, value pairs.

example: ((abc def) (ghi jkl) (a ((b c) (d e))) (mno pqr) (a ((abc def))))

in the above example
abc is key & def is value
ghi is key & jkl is value
a is key & ((b c) (d e)) is value
and so on.


Thanks in advance
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262

If you plug that into Kevin's code, you'll be able to retrive the keys via group(1) and the values via group(2). This works on the one line of sample data you provided, but YMMV. For instance, if you decide you need another level of parenthesis nesting, the regex will become about twice as ugly. I really think you're better off using a dedicated parser for this one.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Regular expressions are not well suited for parsing nested parentheses. You will need a slightly more complicated parser that can count how many "levels" there are in the nesting. If you want to investigate this further on your own, you should google for "context free language", "context free grammar", or "push down automata" as these are the theoretical foundations for doing such parsing.

HTH

Layne


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in writing Regular Expression