I'm parsing a file containing statements line by line. I want to:
- Identify all lines containing assignments.
- Replace identifiers of certain types (Input and Output).
A line is an assignment if it has one of the following two forms:
DataType Identifier = ...
Identifier = ...
The data type must be one of: "R", "L", "H", "X", "I". The data type is optional. Spaces are allowed in any position around the DataType and the Identifier. Example of lines containing statements:
L Input = ...
DigitalOutput = ...
R Output= ...
H AnalogInput=...
X Output = ...
Expected result after parsing the statements above would be:
L Deprecated = ...
DigitalOutput = ...
R Deprecated= ...
H AnalogInput=...
X Deprecated = ...
The file also contains other statements than assignments so its important to identify lines with assignments and only replace identifiers in that case. I've tried to use a regular expression with positive lookbehind and positive lookahead:
public void ReplaceIdentifiers(string line)
{
List<string> validDataTypes = new List<string>{"R", "L", "H", "X", "I"};
List<string> identifiersToReplace = new List<string>{"Input", "Output"};
string = ...
Regex regEx = new Regex(MyRegEx);
regEx.Replace(line, "Deprecated");
}
Where MyRegex is on the form (pseudo code):
$@"(?<=...){Any of the two identifiers to replace}(?=...)"
The lookbehind:
Start of string OR
Zero or more spaces, Any of the valid data types, Zero or more spaces OR
Zero or more spaces
The lookahead:
Zero or more spaces, =
I haven't managed to get the regular expression right. How do I write the regular expression?