I hate regular expressions!

Arrrghhhh!

Sorry....

Hello! :-)

I have a strange long string of which I need to match a regex pattern. This is the string

_XMMLSS.CDcatname:if(nov06cd is not null,if(extended=1 and nov06cd.ra_ext_corr > 0,catname('XLSSCD',nov06cd.ra_ext_corr,nov06cd.dec_ext_corr),catname('XLSSCD',nov06cd.ra_pnt_corr,nov06cd.dec_pnt_corr)).max

and I want to get three groups:

_XMMLSS

CDcatname:if(nov06cd is not null,if(extended=1 and nov06cd.ra_ext_corr > 0,catname('XLSSCD',nov06cd.ra_ext_corr,nov06cd.dec_ext_corr),catname('XLSSCD',nov06cd.ra_pnt_corr,nov06cd.dec_pnt_corr))

max

I've witten this code:

import java.util.regex.*;

publicclass TestRegex{

publicstaticvoid main(String[] args){

String field ="_XMMLSS.CDcatname:if(nov06cd is not null,if(extended=1 and nov06cd.ra_ext_corr > 0,catname('XLSSCD',nov06cd.ra_ext_corr,nov06cd.dec_ext_corr),catname('XLSSCD',nov06cd.ra_pnt_corr,nov06cd.dec_pnt_corr)).max";

String regex ="^(.*?)\\.(.*)\\.(.*)$";

Pattern p = Pattern.compile(regex);

String[] result = p.split(field);

}

}

which uses this pattern: "^(.*?)\\.(.*)\\.(.*)$". I'm sure it is right because I've tested it with Python and it worked as I expected... so... what's wrong? Why it doesn't split my string?

Thank you in advance.

Cheers,

Anhur

[1997 byte] By [Anhura] at [2007-11-27 3:51:14]
# 1
= deleted =Message was edited by: rym82
rym82a at 2007-7-12 8:55:12 > top of Java-index,Java Essentials,Java Programming...
# 2

You don't need split(). You need matches() and then extract the groups.Matcher m = p.matcher(field);

if (m.matches())

{

System.out.println(m.group(1));

System.out.println(m.group(2));

System.out.println(m.group(3));

}

Message was edited by:

sabre150

sabre150a at 2007-7-12 8:55:12 > top of Java-index,Java Essentials,Java Programming...
# 3

Wow, you're right, it works!

I've slightly modified the code like this:

String field = "_XMMLSS.CDcatname:if(nov06cd is not null,if(extended=1 and nov06cd.ra_ext_corr > 0,catname('XLSSCD',nov06cd.ra_ext_corr,nov06cd.dec_ext_corr),catname('XLSSCD',nov06cd.ra_pnt_corr,nov06cd.dec_pnt_corr)).max";

String regex = "^(.*?)\\.(.*)\\.(.*)$";

Pattern p = Pattern.compile(regex);

Matcher m = p.matcher(field);

if (m.matches())

for (int i = 1; i <= m.groupCount(); i++)

System.out.println(m.group(i));

Thank you so much!

Anyway now I have a question: what does split perform? I used split because in my original code actually I split up the string directly ("blabla".split(regex)).

I deduce that split only performs a special regex matching...

Thank you again!

Anhura at 2007-7-12 8:55:12 > top of Java-index,Java Essentials,Java Programming...
# 4

> I've slightly modified the code like this:

:-) 'slightly' is a bit of an understatement!

> Anyway now I have a question: what does split perform?

It 'splits' the string at the separators matched by the regex. It does not create an array element for each matched group (though I can see some advantages to this).

sabre150a at 2007-7-12 8:55:12 > top of Java-index,Java Essentials,Java Programming...
# 5
Clear! Thank you!
Anhura at 2007-7-12 8:55:12 > top of Java-index,Java Essentials,Java Programming...