Java Regular Expressions (regex) Greif
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:
Code:
Pattern pattern = Pattern.compile(".*(\\w+).*");
Matcher matcher = pattern.matcher(buffer);
To get the split string I am trying both this:
Code :
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:
Code :
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)
Re: Java Regular Expressions (regex) Greif
er could you not do something like
Nooo, you could not lol! nvm sorry
Chris
Re: Java Regular Expressions (regex) Greif
I found the answer, this works:
Code :
if(matcher.find()){
System.out.println("found:");
System.out.println(matcher.group());
while(matcher.find()){
System.out.println(matcher.group());
}
}else{
JOptionPane.showMessageDialog(this, "File not formatted correctly.");
return;
}
Re: Java Regular Expressions (regex) Greif
Quote:
Originally Posted by
username9000
I found the answer, this works:
Code :
if(matcher.find()){
System.out.println("found:");
System.out.println(matcher.group());
while(matcher.find()){
System.out.println(matcher.group());
}
}else{
JOptionPane.showMessageDialog(this, "File not formatted correctly.");
return;
}
Hello username9000. Welcome to the Java Programming Forums.
I couldn't get the required results using the regular expression you posted. Is this all you changed to get it to work?
Re: Java Regular Expressions (regex) Greif
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.