Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Greg Sun
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Greg Sun
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21385
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greg Sun
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21385
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic