The regular expression to parse �(abc.3, d.42), (cde.2, 3)�
Greg Sun
Greenhorn
Joined: Nov 01, 2006
Posts: 5
posted
0
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
posted
0
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 ]
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
posted
0
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 ]
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.
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
posted
0
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!!