Skip to content

fix: use re.search instead of re.match in RegexFilter#5368

Open
KBVsent wants to merge 1 commit intoAstrBotDevs:masterfrom
KBVsent:fix/regex-filter-use-search
Open

fix: use re.search instead of re.match in RegexFilter#5368
KBVsent wants to merge 1 commit intoAstrBotDevs:masterfrom
KBVsent:fix/regex-filter-use-search

Conversation

@KBVsent
Copy link
Contributor

@KBVsent KBVsent commented Feb 23, 2026

RegexFilter was using re.match() to evaluate @filter.regex patterns, but re.match() implicitly anchors the match to the start of the string — equivalent to prepending ^ to every pattern. This means patterns without an explicit ^ would silently behave as if one were present, causing messages where the pattern appears mid-sentence to not trigger the handler. This violates standard regex semantics and surprises plugin developers who expect unanchored patterns to match anywhere in the message.

Modifications / 改动点

  • astrbot/core/star/filter/regex.py: Replace self.regex.match(...) with self.regex.search(...) so that unanchored patterns match anywhere in the message. Plugins that require start-of-string matching can still use an explicit ^ anchor.
  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果


Checklist / 检查清单

  • 😊 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。/ If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
  • 👀 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。/ My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
  • 🤓 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到了 requirements.txtpyproject.toml 文件相应位置。/ I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
  • 😮 我的更改没有引入恶意代码。/ My changes do not introduce malicious code.

Summary by Sourcery

Bug Fixes:

  • 确保在未显式使用锚点的情况下,RegexFilter 模式可以匹配消息文本中的任意位置,而不仅仅是从开头开始匹配。
Original summary in English

Summary by Sourcery

Bug Fixes:

  • Ensure RegexFilter patterns without explicit anchors can match anywhere in the message text instead of only at the beginning.

@auto-assign auto-assign bot requested review from Fridemn and Raven95676 February 23, 2026 02:46
@dosubot dosubot bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Feb 23, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @KBVsent, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a subtle but significant issue in the RegexFilter where regex patterns were unintentionally restricted to matching only at the beginning of a string. By transitioning from re.match() to re.search(), the filter now accurately implements standard regular expression behavior, allowing patterns to match substrings found anywhere within the message. This enhancement provides greater flexibility and predictability for plugin developers relying on regex-based filtering.

Highlights

  • Corrected Regex Matching Behavior: The RegexFilter now utilizes re.search() instead of re.match(), which implicitly anchored all patterns to the start of the string. This change ensures that unanchored regex patterns correctly match anywhere within the input message, aligning with standard regex semantics.
Changelog
  • astrbot/core/star/filter/regex.py
    • Replaced self.regex.match with self.regex.search in the filter method to enable unanchored regex matching.
Activity
  • The author has confirmed that this is not a breaking change.
  • The author has indicated that the changes have been well-tested and verified.
  • The author has ensured that no new dependencies were introduced.
  • The author has confirmed that the changes do not introduce malicious code.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@dosubot dosubot bot added the area:core The bug / feature is about astrbot's core, backend label Feb 23, 2026
@dosubot
Copy link

dosubot bot commented Feb 23, 2026

Related Documentation

Checked 1 published document(s) in 1 knowledge base(s). No updates required.

How did I do? Any feedback?  Join Discord

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - 我在这里给出了一些总体反馈:

  • match 切换到 search 会改变所有现有 RegexFilter 用法的行为;请考虑是否需要提供向后兼容的路径(例如通过一个标志位或单独的过滤器),以支持那些隐式依赖“从字符串开头匹配”的插件。
  • 在应用正则之前调用 .strip() 的做法,在你希望对齐标准正则语义的情况下,现在可能会更让人意外;请考虑首尾空白是否应该保留为被搜索字符串的一部分,或者是否应该做成可配置的。
面向 AI 代理的提示
Please address the comments from this code review:

## Overall Comments
- Switching from `match` to `search` changes the behavior for all existing `RegexFilter` usages; consider whether you need a backward-compatibility path (e.g., a flag or separate filter) for plugins that implicitly relied on start-of-string matching.
- The use of `.strip()` before applying the regex may now be more surprising given the intent to align with standard regex semantics; consider whether leading/trailing whitespace should remain part of the string being searched or be made configurable.

Sourcery 对开源项目是免费的——如果你觉得我们的评审有帮助,欢迎分享 ✨
帮我变得更有用!请在每条评论上点击 👍 或 👎,我会根据你的反馈改进后续的评审质量。
Original comment in English

Hey - I've left some high level feedback:

  • Switching from match to search changes the behavior for all existing RegexFilter usages; consider whether you need a backward-compatibility path (e.g., a flag or separate filter) for plugins that implicitly relied on start-of-string matching.
  • The use of .strip() before applying the regex may now be more surprising given the intent to align with standard regex semantics; consider whether leading/trailing whitespace should remain part of the string being searched or be made configurable.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Switching from `match` to `search` changes the behavior for all existing `RegexFilter` usages; consider whether you need a backward-compatibility path (e.g., a flag or separate filter) for plugins that implicitly relied on start-of-string matching.
- The use of `.strip()` before applying the regex may now be more surprising given the intent to align with standard regex semantics; consider whether leading/trailing whitespace should remain part of the string being searched or be made configurable.

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.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly addresses a bug in RegexFilter where re.match was used instead of re.search, causing regex patterns to be incorrectly anchored to the start of the string. The change to re.search is the right solution. My review includes a suggestion to add unit tests for this filter to verify the fix and prevent future regressions.

@KBVsent
Copy link
Contributor Author

KBVsent commented Feb 23, 2026

Hey - 我在这里给出了一些总体反馈:

  • match 切换到 search 会改变所有现有 RegexFilter 用法的行为;请考虑是否需要提供向后兼容的路径(例如通过一个标志位或单独的过滤器),以支持那些隐式依赖“从字符串开头匹配”的插件。
  • 在应用正则之前调用 .strip() 的做法,在你希望对齐标准正则语义的情况下,现在可能会更让人意外;请考虑首尾空白是否应该保留为被搜索字符串的一部分,或者是否应该做成可配置的。

面向 AI 代理的提示
Sourcery 对开源项目是免费的——如果你觉得我们的评审有帮助,欢迎分享 ✨
帮我变得更有用!请在每条评论上点击 👍 或 👎,我会根据你的反馈改进后续的评审质量。
Original comment in English

re.match 来自 Python 早期,当时的设计参考了其他语言的正则 API。它作为一个独立函数保留,也是为了向后兼容。re.match 是一个设计上的冗余,现代项目不应该依赖这个函数。
而插件项目也不应该依赖这个隐形特性来实现字符串开头匹配,应该直接使用r"^pattern"

@zouyonghe zouyonghe self-requested a review February 23, 2026 03:57
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Feb 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend lgtm This PR has been approved by a maintainer size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants