I'm trying to capture assignment operations from a text file using 'java.util.regex.Pattern'. I've been very frustrated trying to fix my regular expression to actually recognize what I am looking for. I've simplified the problem as much as I can and found an issue with picking up white space.
This post proved helpful, and sheds light on issues dealing with the whitespace character set, but does not answer the question of why the following is not working:
Pattern p = Pattern.compile("adfa =");
Scanner sc = new Scanner("adfa =");
if(sc.hasNext(p))
{
String s = sc.next(p);
System.out.println(">" + s + "<");
}
else
System.out.println(":(");
If I try this:
Pattern p = Pattern.compile("\\w+ *=");
The following string is picked up:
"adfa="
But not:
"adfa ="
Simply by making the following change:
Pattern p = Pattern.compile("adfa=");
Scanner sc = new Scanner("adfa=");
All works as intended! Can anyone shed any light on what is going wrong?