File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes The regular expression to parse �(abc.3, d.42), (cde.2, 3)� Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "The regular expression to parse �(abc.3, d.42), (cde.2, 3)�" Watch "The regular expression to parse �(abc.3, d.42), (cde.2, 3)�" New topic
Author

The regular expression to parse �(abc.3, d.42), (cde.2, 3)�

Greg Sun
Greenhorn

Joined: Nov 01, 2006
Posts: 5
Hi,

I want to construct a regular expression to divide the string �(abc.3, d.42), (cde.2, 3)� into 2 groups:
1. abc.3, d.42
2. cde.2, 3
I tried the following regular expression �\\([.*]\\) in String.split(), but it returns an empty array.

Please help me to see what is wrong with my regular expression.

Thanks!

Greg
Greg Sun
Greenhorn

Joined: Nov 01, 2006
Posts: 5
Please ignore this post. I was out of my mind.
I shouldn't use String.split() to get the matching groups.
Instead codes like the following should be used:

[ November 01, 2006: Message edited by: Jim Yingst ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I added code tags to your post, for readability.

Glancing at your code, I think you may well want to try replacing

with

but I'll let you decide for yourself, based on what sort of inputs you actually receive. Consider: what happens if there are two or more consecutive sets of parenthesis? Should the first open parenthsis match up with the first closing parenthesis, or the last? The choice is yours...
[ November 01, 2006: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Greg Sun
Greenhorn

Joined: Nov 01, 2006
Posts: 5
Thanks Jim. I found the Pattern String I used yesterday is still wrong. Here is the test code that I have used.


And I got the following result:


The returned match is not what I intended to have.
I am expecting sth like:


[ November 02, 2006: Message edited by: Greg Sun ]

[ November 02, 2006: Message edited by: Greg Sun ]
[ November 02, 2006: Message edited by: Greg Sun ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18717
    
  40

Greg,

Take a gander at Jim's last post. He already anticipated your problem and gave you the solution. (hint: remember, by default, qualifiers are greedy)

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Greg Sun
Greenhorn

Joined: Nov 01, 2006
Posts: 5
Hi Henry,

Thank you for your reply. I have actually tried out the suggestion given by Jim. But it didn�t work either.

I have also tried out the suggestion given by Ernest in Thread by changing my pattern string to "\\Q(\\E[^)]*\\Q)\\E". But it also gave the same result.
http://www.coderanch.com/t/380534/java/java/Regular-expression-capture-groups

Greg
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
groupCount() tells you how many capturing groups there are in the regex, not how many times a group matched. If you want to match both of the parenthesized sequences in one pass, you have to use a regex that matches both of them; Also, the group numbers start at one, not zero, group(0) being a special case that returns the whole match. That means the highest group number is the same as the groupCount, so you have define the for loop differently than you usually do: This will print the whole match and the contents of each capturing group. If you don't want the whole match, start at one instead of zero.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18717
    
  40

Originally posted by Greg Sun:
Hi Henry,

Thank you for your reply. I have actually tried out the suggestion given by Jim. But it didn�t work either.

I have also tried out the suggestion given by Ernest in Thread by changing my pattern string to "\\Q(\\E[^)]*\\Q)\\E". But it also gave the same result.
http://www.coderanch.com/t/380534/java/java/Regular-expression-capture-groups

Greg



Greg,

I knew something bothered me, but I couldn't place it -- until now.

You can't uses matches() for this. The capturing group number is dependent on the location in the pattern, not when they are captured.

So, you can't use it to match multiple groups when you only have one group in the pattern. Basically, even if you get it working, only the last match will be returned as every match is captured as group 1.

Anyway, the solution is to use find() instead. And since this is probably not a homework question...



Henry
Greg Sun
Greenhorn

Joined: Nov 01, 2006
Posts: 5
Thanks Alan and Henry. Ah... yes!!! My understanding of Matcher class needs some boost. After I changed the code to use find(), it works! Thank you so much for your help!!
 
Don't get me started about those stupid light bulbs.
 
subject: The regular expression to parse �(abc.3, d.42), (cde.2, 3)�