Home > Backend Development > Golang > Why Isn\'t My Regex Negated Set Working in Go?

Why Isn\'t My Regex Negated Set Working in Go?

Barbara Streisand
Release: 2024-10-26 03:55:27
Original
478 people have browsed it

 Why Isn't My Regex Negated Set Working in Go?

Regex Expression Negated Set Not Working in Go

A user has encountered an issue where a regular expression with a negated set was not working as expected in Go. The expression, verified on multiple online regex parsers, was intended to match strings that did not end in specific file extensions.

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

However, implementing this expression in Go yielded different results. The user simplified the expression to remove the negated set and obtained desired matches.

Understanding the Issue

Go's regular expression engine, RE2, does not support lookaround operators like the negative lookahead ?! used in the original expression. This absence of lookaround support caused the negated set to be ineffective.

Solution

To ensure that a string ends in a three-character file extension, the user can simplify the expression to:

\.\w{3}$
Copy after login

This expression matches a literal period, followed by three word characters (alphanumeric and underscore), followed by the end of the string. It effectively achieves the same result as the original expression but without relying on lookaround operators.

The above is the detailed content of Why Isn\'t My Regex Negated Set Working in Go?. 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