Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8276507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:23:51+00:00 2026-06-08T08:23:51+00:00

Is there an implementation of regular expressions in Python/PHP/JavaScript that supports variable-length lookbehind-assertion? /(?<!foo.*)bar/

  • 0

Is there an implementation of regular expressions in Python/PHP/JavaScript that supports variable-length lookbehind-assertion?

/(?<!foo.*)bar/

How can I write a regular expression that has the same meaning, but uses no lookbehind-assertion?

Is there a chance that this type of assertion will be implemented some day?

Things are much better that I thought.

Update:

(1) There are regular expressions implementation that support variable-length lookbehind-assertion already.

Python module regex (not standard re, but additional regex module) supports such assertions (and has many other cool features).

>>> import regex
>>> m = regex.search('(?<!foo.*)bar', 'f00bar')
>>> print m.group()
bar
>>> m = regex.search('(?<!foo.*)bar', 'foobar')
>>> print m
None

It was a really big surprise for me that there is something in regular expressions that Perl can’t do and Python can. Probably, there is “enhanced regular expression” implementation for Perl also?

(Thanks and +1 to MRAB).

(2) There is a cool feature \K in modern regular expressions.

This symbols means that when you make a substitution (and from my point of view the most interesting use case of assertions is the substitution), all characters that were found before \K must not be changed.

s/unchanged-part\Kchanged-part/new-part/x

That is almost like a look-behind assertion, but not so flexible of course.

More about \K:

  • Perl Regular Expression \K Trick
  • PCRE Regex Spotlight: \K

As far as I understand, you can’t use \K twice in the same regular expression. And you can’t say till which point you want to “kill” the characters that you’ve found. That is always till the beginning of the line.

(Thanks and +1 to ikegami).

My additional questions:

  • Is it possible to say what point must be the final point of \K effect?
  • What about enhanced regular expressions implementations for Perl/Ruby/JavaScript/PHP? Something like regex for Python.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T08:23:54+00:00Added an answer on June 8, 2026 at 8:23 am

    Most of the time, you can avoid variable length lookbehinds by using \K.

    s/(?<=foo.*)bar/moo/s;
    

    would be

    s/foo.*\Kbar/moo/s;
    

    Anything up to the last \K encountered is not considered part of the match (e.g. for the purposes of replacement, $&, etc)

    Negative lookbehinds are a little trickier.

    s/(?<!foo.*)bar/moo/s;
    

    would be

    s/^(?:(?!foo).)*\Kbar/moo/s;
    

    because (?:(?!STRING).)* is to STRING as [^CHAR]* is to CHAR.


    If you’re just matching, you might not even need the \K.

    /foo.*bar/s
    
    /^(?:(?!foo).)*bar/s
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an efficient implementation of matlab's deconv in python? # Convolve z=conv(x, y)
Is there a JavaScript implementation of the OpenVG standard based on WebGL? I'm pretty
It seems that there is a implementation of rope in my /usr/include/c++/4.5.1/ext/rope (and ropeimpl.h
Does anyone know of a way of indexing regular expressions , so that I
Is there some implementation of java.util.Map that does not uses HashCode? I have the
We have a configuration file that lists a series of regular expressions used to
A question that I answered got me wondering: How are regular expressions implemented in
Regular expressions are often pointed to as the classical example of a language that
There is cgi.escape but that appears to be implemented in pure python. It seems
I am building an application that allows you to apply styles to regular expressions

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.