Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Matching a left square bracket at beginning of file

Vasco Saavedra
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following properties file :




I'm trying to parse this file, and obtain the properties for each group, like:
PROPERTIES_GROUP_1 = {prop1=val1;prop2=val2}
PROPERTIES_GROUP_2 = {prop3=val3;}

I'm using the Java 5 Scanner class to parse this file.
I'm trying to use the beginning of each group as the scanner delimiter:

private static final StringPATTERN= "^\\[.*";
File file= new File(filename);
scanner= new Scanner(file);



The main problem is that i can't match the beginning of a group property.

I've tried several tokens, like:

PATTERN= "^\\[.*";
PATTERN= "^[\\[.*]";
PATTERN= "^\\[";


But none of this worked.
So my question is, HOW can i match the beginning of a property group ?

Best regards...
Jim Yingst
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the main problem is that when you call useDelimiter(), the scanner will look for the itemsbetween the delimiters. E.g. if you have a line


and you use a delimiter of ",", then Scanner's next() will give you all the numbers, not the commas. If you want to see the commas, you can't use them as delimiters. Instead try using findWithinHorizon().

If you want to find one complete properties group tag, for example, try

However as I look at your problem, it may well be easier to forget about regexes, and instead just read one line at a time. You can look at the first letter of each line to tell what's going on:

'[' - starting a new property group.
' ' or blank line - ending a property group
any other letter - listing a new property within the current group
[ May 29, 2008: Message edited by: Jim Yingst ]
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic