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

  • Home
  • SEARCH
  • 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 930177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:17:36+00:00 2026-05-15T20:17:36+00:00

How to construct a regular expression search pattern to find string1 that is not

  • 0

How to construct a regular expression search pattern to find string1 that is not followed by string2 (immediately or not)?

For for instance, if string1=”MAN” and string2=”PN”, example search results would be:

"M": Not found
"MA": Not found
"MAN": Found
"BLAH_MAN_BLEH": Found
"MAN_PN": Not found
"BLAH_MAN_BLEH_PN": Not found
"BLAH_MAN_BLEH_PN_MAN": Not found

Ideally, a one-linear search, instead of doing a second search for string2.

PS: Language being used is 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-05-15T20:17:37+00:00Added an answer on May 15, 2026 at 8:17 pm

    It looks like you can use MAN(?!.*PN). This matches MAN and uses negative lookahead to make sure that it’s not followed by PN (as seen on rubular.com).

    Given MAN_PN_MAN_BLEH, the above pattern will find the second MAN, since it’s not followed by PN. If you want to validate the entire string and make sure that there’s no MAN.*PN, then you can use something like ^(?!.*MAN.*PN).*MAN.*$ (as seen on rubular.com).

    References

    • regular-expressios.info/Lookarounds

    Related questions

    • How can I check if every substring of four zeros is followed by at least four ones using regular expressions?

    Non-regex option

    If the strings are to be matched literally, then you can also check for indices of substring occurrences.

    In Python, find and rfind return lowest and highest index of substring occurrences respectively.

    So to make sure that string1 occurs but never followed by string2, and both returns -1 if the string is not found, so it looks like you can just test for this condition:

    string.rfind(s, string2) < string.find(s, string1)
    

    This compares the leftmost occurrence of string1 and the rightmost occurrence of string2.

    • If neither occurs, both are -1, and result is false
    • If string1 occurs, but string2 doesn’t, then result is true as expected
    • If both occurs, then the rightmost string2 must be to the left of the leftmost string1
      • That is, no string1 is ever followed by string2

    API links

    • find
    • rfind
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would I construct a regular expression to find all words that end in
I'm trying to construct a regular expression that would match a pattern as such:
I'm trying to construct a regular expression that would extract the name of the
Is it possible to construct a PCRE-style regular expression that will only match each
I am trying to construct a regular expression that will match the following URL:
I would like to construct a regular expression that matches any letter (including accented
Is is possible to construct a regular expression that rejects all input strings?
How do I construct a regular expression to search a larger string for a
I want to construct a regular expression that will, for every line of text
I am trying to construct a regular expression that the total number of a's

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.