Skip to content

Add regex match option for StringFilter#8178

Open
MaoTouZhu wants to merge 1 commit into
dotnetcore:mainfrom
MaoTouZhu:main
Open

Add regex match option for StringFilter#8178
MaoTouZhu wants to merge 1 commit into
dotnetcore:mainfrom
MaoTouZhu:main

Conversation

@MaoTouZhu

@MaoTouZhu MaoTouZhu commented Jul 1, 2026

Copy link
Copy Markdown

Link issues

fixes #8177

Summary By Copilot

Regression?

  • Yes
  • [ ☑️ ] No

Risk

  • High
  • Medium
  • [ ☑️ ] Low

Verification

  • [ ☑️ ] Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • [ ☑️ ] N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

Summary by Sourcery

Add support for regex-based string filtering in query components.

New Features:

  • Introduce a Regex filter action for string fields to enable regular expression matching in queries.
  • Expose the Regex option in StringFilter and QueryBuilder filter operators for user selection.

Enhancements:

  • Extend the expression-building infrastructure to handle Regex filter expressions via Regex.IsMatch.
  • Add localization entries for the new Regex filter operator in supported languages.

@bb-auto

bb-auto Bot commented Jul 1, 2026

Copy link
Copy Markdown

Thanks for your PR, @MaoTouZhu. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@sourcery-ai

sourcery-ai Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adds a new Regex string filter option that compiles to a Regex.IsMatch expression and wires it into the filtering UI and localization.

Sequence diagram for applying Regex StringFilter

sequenceDiagram
    actor User
    participant StringFilter
    participant QueryBuilder
    participant LambdaExtensions
    participant Regex

    User->>StringFilter: OnParametersSet()
    StringFilter->>StringFilter: SelectedFilterItems add Regex

    User->>QueryBuilder: select Regex filter and value
    QueryBuilder->>LambdaExtensions: GetExpression(filter)
    LambdaExtensions->>LambdaExtensions: RegexMatch(left, right)
    LambdaExtensions->>Regex: IsMatch(left, right)
    Regex-->>LambdaExtensions: bool
    LambdaExtensions-->>QueryBuilder: Expression
    QueryBuilder-->>User: filtered results based on Regex
Loading

File-Level Changes

Change Details Files
Add support in expression-building pipeline for a new Regex filter action on string fields.
  • Extend FilterAction enum with a Regex value and description metadata.
  • Update GetExpression to map FilterAction.Regex to a new RegexMatch helper.
  • Implement RegexMatch helper that builds a MethodCallExpression calling Regex.IsMatch(string, string).
  • Import System.Text.RegularExpressions to access Regex.IsMatch.
src/BootstrapBlazor/Extensions/LambdaExtensions.cs
src/BootstrapBlazor/Enums/FilterAction.cs
Expose the Regex filter option in string filter and query builder UIs with localization support.
  • Add a 'Regex' option to the StringFilter component's list of selectable filter actions.
  • Add a 'Regex' option to the QueryBuilder component's string filter operator list.
  • Add corresponding 'Regex' localization entries for both English and Chinese locales.
src/BootstrapBlazor/Components/Filters/StringFilter.razor.cs
src/BootstrapBlazor/Components/QueryBuilder/QueryBuilder.razor.cs
src/BootstrapBlazor/Locales/en.json
src/BootstrapBlazor/Locales/zh.json

Assessment against linked issues

Issue Objective Addressed Explanation
#8177 Add a regex-based filter operation to the filtering logic so that StringFilter can evaluate values using regular expressions.
#8177 Expose the regex match option in the StringFilter (and related query UI) so users can select it, including necessary enum values and localization entries.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@bb-auto bb-auto Bot added the enhancement New feature or request label Jul 1, 2026
@bb-auto bb-auto Bot requested a review from ArgoZhang July 1, 2026 18:56
@bb-auto bb-auto Bot added this to the v10.7.0 milestone Jul 1, 2026

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • The new RegexMatch expression doesn’t guard against left being null (unlike ContainsWidthComparison), which could lead to NullReferenceExceptions at runtime; consider adding a null check similar to the Contains implementation.
  • Using a user-provided pattern directly in Regex.IsMatch can throw ArgumentException on invalid patterns; consider validating or safely handling invalid regex patterns before constructing the expression to avoid runtime failures.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new `RegexMatch` expression doesn’t guard against `left` being null (unlike `ContainsWidthComparison`), which could lead to `NullReferenceException`s at runtime; consider adding a null check similar to the `Contains` implementation.
- Using a user-provided pattern directly in `Regex.IsMatch` can throw `ArgumentException` on invalid patterns; consider validating or safely handling invalid regex patterns before constructing the expression to avoid runtime failures.

## Individual Comments

### Comment 1
<location path="src/BootstrapBlazor/Extensions/LambdaExtensions.cs" line_range="245-248" />
<code_context>
             FilterAction.LessThanOrEqual => Expression.LessThanOrEqual(left, right),
             FilterAction.Contains => left.Contains(right, comparison),
             FilterAction.NotContains => Expression.Not(left.Contains(right, comparison)),
+            FilterAction.Regex => left.RegexMatch(right),
             _ => filter.FieldValue switch
             {
</code_context>
<issue_to_address>
**issue (bug_risk):** Regex matching does not guard against null values, unlike the Contains branch.

The `ContainsWidthComparison` helper wraps its call in a null-check (`left != null && ...`), but the new `FilterAction.Regex` path calls `left.RegexMatch(right)` without any guard. If `left` is null or not a string, this can throw at runtime. Please add consistent null-handling (and any needed string conversion) in the regex branch to match the other string-based filters.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +245 to 248
FilterAction.Regex => left.RegexMatch(right),
_ => filter.FieldValue switch
{
LambdaExpression t => Expression.Invoke(t, left),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Regex matching does not guard against null values, unlike the Contains branch.

The ContainsWidthComparison helper wraps its call in a null-check (left != null && ...), but the new FilterAction.Regex path calls left.RegexMatch(right) without any guard. If left is null or not a string, this can throw at runtime. Please add consistent null-handling (and any needed string conversion) in the regex branch to match the other string-based filters.

@MaoTouZhu

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(StringFitlter): Add regex match option for StringFitlter.

2 participants