Can We Achieve True Variable-Length Lookbehind in Regular Expressions?

Mary-Kate Olsen
Release: 2024-11-02 10:05:02
Original
934 people have browsed it

 Can We Achieve True Variable-Length Lookbehind in Regular Expressions?

Variable-Length Lookbehind-Assertion Alternatives for Regular Expressions

Variable-length lookbehind assertions in regular expressions, denoted by (?

Python with regex Module

The Python regex module offers support for variable-length lookbehind assertions.

<code class="python">import regex

m = regex.search('(?<!foo.*)bar', 'f00bar')
print(m.group())  # Output: bar</code>
Copy after login

K Token

Modern regular expressions introduce the K token, which ensures that matching characters before it are not included in the match.

s/unchanged-part\Kchanged-part/new-part/x
Copy after login

Limitations

While K provides some lookbehind functionality, it cannot remove characters up to a specific point or be used multiple times in an expression.

Enhanced Regular Expression Implementations

Besides regex for Python, other enhanced regular expression implementations may exist for Perl, Ruby, JavaScript, and PHP. Investigating these alternatives may provide additional options for handling variable-length lookbehind assertions.

Questions

  • Can K's effect be extended to a specific point in the match?
  • Are there any enhanced regular expression implementations for Perl, Ruby, JavaScript, or PHP that support variable-length lookbehind assertions?

The above is the detailed content of Can We Achieve True Variable-Length Lookbehind in Regular Expressions?. 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