# How to Use Comparison Operators with CSS‑Like Selectors in OfficeCLI

> Learn to use comparison operators like =, !=, >, <, >=, <= with CSS-like selectors in OfficeCLI to filter document elements by attributes. Master data manipulation effectively.

- Repository: [OfficeAI/OfficeCLI](https://github.com/iofficeai/OfficeCLI)
- Tags: how-to-guide
- Published: 2026-07-08

---

**OfficeCLI supports six comparison operators (`=`, `!=`, `>`, `<`, `>=`, `<=`) within CSS‑like selectors to filter document elements by numeric or lexical attributes.**

OfficeCLI provides a powerful query interface for Office documents using a CSS‑like selector syntax. This article explains how to use comparison operators with CSS‑like selectors in OfficeCLI to precisely target paragraphs, runs, and other elements in Word, Excel, and PowerPoint files. The selector engine parses these operators in [`WordHandler.Selector.cs`](https://github.com/iOfficeAI/OfficeCLI/blob/main/WordHandler.Selector.cs) and evaluates them through the `AttributeFilter` class according to the iOfficeAI/OfficeCLI source code.

## Understanding the Selector Syntax

OfficeCLI selectors mirror CSS attribute selectors. A basic selector consists of an **element name** followed by one or more **attribute filters** enclosed in square brackets:

```

element[attribute operator value]

```

You can chain multiple filters on a single element (AND logic) or use the child combinator (`>`) to target nested elements:

```

element[attr1=value1][attr2>value2] > child[attr3!=value3]

```

## Supported Comparison Operators

The parser recognizes the following operators inside attribute brackets:

| Operator | Meaning |
|----------|---------|
| `=` | Exact equality (default) |
| `!=` | Not‑equal |
| `>` | Greater‑than |
| `<` | Less‑than |
| `>=` | Greater‑than‑or‑equal |
| `<=` | Less‑than‑or‑equal |

These operators work for both **numeric comparisons** (font sizes, line heights) and **lexical comparisons** (style names, text content).

## How the Selector Engine Parses Operators

According to the source code in [`src/officecli/Handlers/Word/WordHandler.Selector.cs`](https://github.com/iOfficeAI/OfficeCLI/blob/main/src/officecli/Handlers/Word/WordHandler.Selector.cs), the selector parser uses a regular expression to capture the operator:

```csharp
@"\[@?(\w+)(\\?!?=)([^\]]+)\]"

```

This regex extracts the attribute name, the operator (with any leading backslash removed), and the value. After parsing via `ParseSelector` and `SplitChildCombinator`, the resulting `SelectorPart` objects are passed to the **attribute‑filter engine** implemented in [`src/officecli/Core/AttributeFilter.cs`](https://github.com/iOfficeAI/OfficeCLI/blob/main/src/officecli/Core/AttributeFilter.cs).

The `AttributeFilter` class normalizes values (e.g., stripping `#` from color codes), converts operators into concrete comparisons (using `double.TryParse` for numbers or case‑insensitive string comparison), and returns a boolean indicating whether the document node satisfies the filter.

## Practical Use Cases and Examples

### Numeric Comparisons

Filter elements by measurable properties like font size or row height:

```bash

# Select paragraphs with font size 14 pt or larger

officecli query "paragraph[size>=14pt]"

# Find table rows taller than 50 points

officecli query "row[height>50]"

```

### Lexical Comparisons

Compare string attributes such as style names using alphabetical ordering:

```bash

# Select paragraphs whose style name sorts before "Heading 3"

officecli query "paragraph[style<\"Heading 3\"]"

```

### Negation

Exclude elements that match a specific condition:

```bash

# List runs that are NOT bold

officecli query "run[bold!=true]"

```

### Combined Filters

Chain multiple conditions to narrow results:

```bash

# Centered paragraphs larger than 12 pt

officecli query "paragraph[align=center][size>12pt]"

```

### Child Combinator

Apply filters to child elements while maintaining parent context:

```bash

# Italic runs inside Heading 2 paragraphs

officecli query "paragraph[style=Heading2] > run[italic=true]"

```

## Implementation Details: From Parser to Filter

The query execution flow involves several key components in the iOfficeAI/OfficeCLI repository:

1. **Entry Point**: [`CommandBuilder.Query.cs`](https://github.com/iOfficeAI/OfficeCLI/blob/main/CommandBuilder.Query.cs) receives the `officecli query` command and forwards the selector string.
2. **Parsing**: [`WordHandler.Selector.cs`](https://github.com/iOfficeAI/OfficeCLI/blob/main/WordHandler.Selector.cs) contains `ParseSelector` and `SplitChildCombinator` methods that tokenize the selector into `SelectorPart` objects, extracting element names, attributes, operators, and values.
3. **Evaluation**: [`AttributeFilter.cs`](https://github.com/iOfficeAI/OfficeCLI/blob/main/AttributeFilter.cs) performs the actual comparison logic, handling type coercion between strings and numbers and interpreting the operator stored from the regex capture group.

This architecture ensures that adding new comparison operators or attribute types requires minimal changes to the core parser.

## Summary

- OfficeCLI selectors use CSS‑like syntax with element names and bracketed attribute filters.
- Six comparison operators are supported: `=`, `!=`, `>`, `<`, `>=`, and `<=`.
- The parser in [`WordHandler.Selector.cs`](https://github.com/iOfficeAI/OfficeCLI/blob/main/WordHandler.Selector.cs) extracts operators using a specific regex pattern before evaluation.
- Numeric comparisons use `double.TryParse`, while lexical comparisons use case‑insensitive string ordering.
- Filters can be chained with AND logic and combined with child combinators (`>`) for complex queries.

## Frequently Asked Questions

### Can I use comparison operators with Excel and PowerPoint documents, or only Word?

The selector syntax and comparison operators work across all Office document types supported by OfficeCLI. While [`WordHandler.Selector.cs`](https://github.com/iOfficeAI/OfficeCLI/blob/main/WordHandler.Selector.cs) handles Word‑specific parsing, the core [`AttributeFilter.cs`](https://github.com/iOfficeAI/OfficeCLI/blob/main/AttributeFilter.cs) logic is document‑agnostic and reused for Excel and PowerPoint handlers.

### Are string comparisons case‑sensitive when using operators like `<` or `>`?

No, lexical comparisons performed by the `AttributeFilter` engine are case‑insensitive. The implementation uses standard case‑insensitive string comparison methods to ensure that `style="Heading1"` matches `style="heading1"` regardless of capitalization.

### How do I escape special characters in selector values?

When attribute values contain spaces or special characters, wrap them in double quotes. For example: `paragraph[style="Heading 1"]`. The parser in [`WordHandler.Selector.cs`](https://github.com/iOfficeAI/OfficeCLI/blob/main/WordHandler.Selector.cs) handles quoted values within the regex capture group `([^\]]+)`.

### Can I mix different operators in a single selector?

Yes, you can chain multiple attribute filters on the same element, and each can use a different operator. The engine treats chained filters as AND conditions. For example, `paragraph[size>=12pt][size<=14pt][style!=Quote]` selects paragraphs between 12 and 14 points that are not styled as quotes.