• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Regex and Java

DanDaManJC

Senior member
Hey guys,

I'm trying to process a CSV that has some garbage data plus the data I actually want. So in short, I want to find the lines that look like this:
,,,,,ATT,,,PM,,Date,,G,,G,,G,G,,
where the four Gs could be G, R, Y or -. So I'm using the regex expression: [GRY-],,.

I've tested the expression against the string at regexplanet.com and the pattern is found in the string... but for some reason it's not working out in java, which leads me to believe I may be using one of the classes improperly. That said, I haven't found my error either, so a helping hand would be cool 🙂

Thanks!

Code:
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class regextest {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Pattern dashboard = Pattern.compile("[GRY-],,");
		Matcher fit = dashboard.matcher(",,,,,ATT,,,PM,,Date,,G,,G,,G,G,,");
		if(fit.matches()){
			System.out.println("found G");
		} else {
			System.out.println("didnt find G");
		}
	}

}

edit:
problem was i was using the matches() method as opposed to the find() method
 
Last edited:
Back
Top