This document describes how to use the CsvConditionParser to parse condition definitions from strings and create CsvCondition objects for filtering CSV data.
The CsvConditionParser allows you to define filtering conditions using a natural string syntax instead of manually creating condition objects. It supports complex expressions with logical operators, parentheses for grouping, and various comparison operations.
- Use square brackets to reference columns:
[column name]or[column index] - Examples:
[name],[age],[0],[1]
contains: Check if one string contains anotherequalsor=: Check for exact string equalitymatches: Check against a regular expression pattern
=: Equal to!=: Not equal to (note: not yet implemented in all condition classes)<: Less than<=: Less than or equal to>: Greater than>=: Greater than or equal to
AND: Logical AND operationOR: Logical OR operationNOT: Logical NOT operation
(): Use parentheses to group expressions and control precedence
- String literals:
"value"or'value' - Numeric literals:
123,123.45
"[status] = 'active'" // String equality
"[age] >= 30" // Numeric comparison
"[name] contains 'John'" // String contains"[column A] contains [column B]" // One field contains another
"[salary] >= [minimum_salary]" // Numeric field comparison
"[first_name] = [last_name]" // Field equality"[status] = 'active' AND [age] >= 30"
"[department] = 'Engineering' OR [department] = 'Marketing'"
"NOT [status] = 'inactive'""([age] < 30 AND [salary] > 50000) OR ([department] = 'Management' AND [status] = 'active')"
"([department] = 'Engineering' OR [department] = 'Marketing') AND [salary] >= 60000"import org.rappsilber.data.csv.CsvParser;
import org.rappsilber.data.csv.condition.CsvCondition;
import org.rappsilber.data.csv.condition.CsvConditionParser;
// Create your CSV parser
CsvParser csvParser = new CsvParser();
csvParser.openFile(csvFile, true); // true = has header
// Create the condition parser
CsvConditionParser conditionParser = new CsvConditionParser(csvParser);
// Parse a condition string
String conditionString = "[status] = 'active' AND [age] >= 30";
CsvCondition condition = conditionParser.parse(conditionString);
// Use the condition while reading CSV data
while (csvParser.next()) {
if (condition.fits(csvParser)) {
// Process matching row
System.out.println("Matching row: " + getCurrentRowData(csvParser));
}
}The parser integrates seamlessly with your existing CSV condition framework:
- All parsed conditions implement the
CsvConditioninterface - Supports both
fits(CsvParser csv)andfits(int row, CSVRandomAccess csv)methods - Works with existing condition classes like
CsvConditionAnd,CsvConditionOr, etc. - Automatically resolves column names using the CSV parser's header information
The parser throws CsvConditionParser.ParseException for various parsing errors:
- Syntax errors in the expression
- Unknown column names
- Invalid operators
- Mismatched parentheses
- Invalid numeric values
Example error handling:
try {
CsvCondition condition = conditionParser.parse(conditionString);
// Use condition...
} catch (CsvConditionParser.ParseException e) {
System.err.println("Failed to parse condition: " + e.getMessage());
}The parser follows standard logical operator precedence:
- Parentheses
()(highest precedence) - NOT operations
- Comparison operations (
=,<,>,contains, etc.) - AND operations
- OR operations (lowest precedence)
- The parser creates condition objects that are optimized for repeated evaluation
- Column name resolution is cached in the CSV parser
- Complex expressions with many OR conditions may be slower than AND conditions
- Consider using parentheses to optimize evaluation order for complex expressions
To add support for new operators or condition types:
- Add the operator to the tokenization patterns in
CsvConditionParser.tokenize() - Add the operator handling in
CsvConditionParser.createCondition() - Implement the corresponding
CsvConditionclass if it doesn't exist
- String patterns for
matchesoperator must be valid Java regular expressions - Field-to-field operations require both fields to exist in the CSV
- Numeric comparisons will attempt to parse string values as doubles
- Case sensitivity depends on the underlying string operations
See CsvConditionUsageExample.java for comprehensive usage examples and test cases.