PDA

View Full Version : Java Regular Expression Grief


probe_scv_drone
06-10-2009, 09:41 PM
Hi, I'm trying to parse some text with java's regex and nothing seems to be working -_-;;;
In this code all I am trying to do is parse a string for words and it doesn't work:
Pattern pattern = Pattern.compile(".*(\\w+).*");
Matcher matcher = pattern.matcher(buffer);

To get the split string I am trying both this:
String[] matches = pattern.split(buffer);
System.out.println("split produced:");
for(int i = 0; i < matches.length; i++){
System.out.println(matches[i]);
}

and this:
matcher.find(0);
System.out.println(matcher.group());
while(matcher.find()){
System.out.println(matcher.group(1));
}

What I am tyring to do is split a string like this:
Time IRIGB (s) Lat Ltn (deg) Lon Ltn (deg) Tas stbd (m/s) wind speed Hg (m/s) wind direction Hg (deg t)

into substrings like this:
Time IRIGB (s)
Lat Ltn (deg)
Lon Ltn (deg)
Tas stbd (m/s)
wind speed Hg (m/s)
wind direction Hg (deg t)

rajesh.berugu
06-10-2009, 10:57 PM
nice coding

probe_scv_drone
06-12-2009, 01:06 AM
I was able to split what I wanted by doing this:

Pattern pattern = Pattern.compile("(\\w[\\w|\\s]+\\([\\w|\\s|\\/]*\\))");
Matcher matcher = pattern.matcher("Time IRIGB (s) Lat Ltn (deg) Lon Ltn (deg) Tas stbd (m/s) wind speed Hg (m/s) wind direction Hg (deg t)");

Then I do a loop to get the substring matches from the line:

System.out.println("found:");
System.out.println(matcher.group());
while(matcher.find()){
System.out.println(matcher.group());
}

The program outputs this:
found:
Time IRIGB (s)
Lat Ltn (deg)
Lon Ltn (deg)
Tas stbd (m/s)
wind speed Hg (m/s)
wind direction Hg (deg t)

explaining the regular expression "(\\w[\\w|\\s]+\\([\\w|\\s|\\/]*\\))"
- starts with a word character ( an alphanumeric ) \\w
- then 1 or more alphanumerics or spaces [\\w|\\s]+
- then a open parentheses ( \\(
- then 0 or more alphanumerics or spaces or /'s [\\w|\\s|\\/]
- then a close parentheses ) \\)
- the brackets around the entire expression means that thats what I want to pull out of the string with match.group() and match.find()