How Can I Match Strings Without Specific File Extensions in Go\'s Regex Engine?

Susan Sarandon
Release: 2024-10-26 07:35:30
Original
432 people have browsed it

How Can I Match Strings Without Specific File Extensions in Go's Regex Engine?

Understanding Negated Sets in Go's Regex Engine

In Go, the standard library's regular expression engine (RE2) lacks support for lookarounds, including the negative lookahead operator ?! as used in the provided regular expression:

^(.*\.(?!(htm|html|class|js)$))?[^.]
Copy after login

This regex aims to match strings that do not end with specific file extensions. However, in Go, it does not function as expected due to the lack of lookaround support.

Alternative Solutions

Instead of relying on a negated set, there are alternative solutions to handle this scenario in Go:

  1. Verify File Extension Directly:
    Replace the negated set with a direct check for the desired file extensions:

    re.MustCompile(`(type1|type2)_(\d+)\.(csv|ini)`)
    Copy after login
  2. Remove Trailing Period:
    If the goal is to ensure the string ends with a three-character file extension, without any additional characters, a simplified expression can be used:

    re.MustCompile(`\.\w{3}$`)
    Copy after login

Matching Behavior

By removing the negated set or using an alternative syntax, Go's regex engine will accurately match the desired strings as expected.

The above is the detailed content of How Can I Match Strings Without Specific File Extensions in Go\'s Regex Engine?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!