Yeah, my input string was "Time IRIGB (s) Lat Ltn (deg) Lon Ltn (deg) Tas stbd (m/s) wind speed Hg (m/s) wind direction Hg (deg t)"
and my program output was:
Time IRIGB (s)
Lat Ltn (deg)
Lon Ltn (deg)
Tas stbd (m/s)
wind speed Hg (m/s)
wind direction Hg (deg t)
I had to change my regular expression, like the string in Pattern.compile().
I changed it to "(\\w[\\w|\\s]+\\([\\w|\\s|\\/]*\\))" which means:
- starts with a word character ( an alphanumeric )
- then 1 or more alphanumerics or spaces
- then a open parentheses (
- then 0 or more alphanumerics or spaces or /'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()
Yeah, it took me like an hour to figure out that you need \\ cause you have to escape the slash in the string then escape your actual character class cause java's strings use \ as a special character grrr. But pretty much how you use java's regex is like this:
- if you just want to split a string with a simple delimiter you can create a pattern via:
REGEX = " ";
Pattern p = Pattern.compile(REGEX);
String[] outputs = p.split(yourInputString);
this will split the string by spaces
- but, if you want to split a string into things separated more complex then just spaces you have to do the way I did it, compiling an actual regular expression as opposed to just the delimiter used to split a string, except you'll now have to use the Matcher class and do matcher.find() to find the location of the first match and then matcher.group() ( matcher.group also can have int parameters which specify more complex regular expressions where you have matches within matches that you want to pull out ) then matcher.find() for the next one and another matcher.group() and so on, finally matcher.find() will return false when there is nothing more to search.