Reading a text config file: using regex to parse
Looking for a way to read the following config file sample using a multi line regex matcher. I could just read in the file by line, but I want to get decent with the specifics of flexible regular expression matching.
So the config file is filled with blocks of code as follows:
blockName BLOCK
IDENTIFIER value
IDENTIFIER value
IDENTIFIER
"string literal value that
could span multiple lines"
The number of identifiers could be from 1..infinity. IDENTIFIER could be NAME, DESCRIPTION, TYPE, or the like.
I have never worked with multi line regular expressions before. I'm not very familiar with the process. I essentially want to use a findAll function using this regular expression to put all of the parsed block data into a data structure 开发者_运维百科for processing.
EDIT: clarification: I'm only looking to read this file once. I do not care about efficiency or elegance. I want to read the information into a data structure and then spit it out in a different format. It is a large file (3000 lines) and I don't want to do this by hand.
I don't think regex is the best tool for this.
Try this, which should work in perl regular expressions:
([\w\d]*)\s+BLOCK\s*\n(\s*(NAME|DESCRIPTION|TYPE|...)\s*([\w\d]*|"(.*)")\s*\n)+
I verified it at REGex TESTER using the following test text:
blockName BLOCK
NAME value
NAME value
DESCRIPTION
"string literal value that
could span multiple lines"
otherName BLOCK
NAME value
TYPE value
DESCRIPTION
"string literal value that
could span multiple lines"
It will only find the last block/identifier if the file ends in a newline
精彩评论