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 114531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:52:31+00:00 2026-05-11T02:52:31+00:00

I know it’s possible to match a word and then reverse the matches using

  • 0

I know it’s possible to match a word and then reverse the matches using other tools (e.g. grep -v). However, is it possible to match lines that do not contain a specific word, e.g. hede, using a regular expression?

Input:
hoho hihi haha hede 
Code:
grep "<Regex for 'doesn't contain hede'>" input 
Desired output:
hoho hihi haha 
  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T02:52:32+00:00Added an answer on May 11, 2026 at 2:52 am

    The notion that regex doesn’t support inverse matching is not entirely true. You can mimic this behavior by using negative look-arounds:

    ^((?!hede).)*$ 

    The regex above will match any string, or line without a line break, not containing the (sub)string ‘hede’. As mentioned, this is not something regex is "good" at (or should do), but still, it is possible.

    And if you need to match line break chars as well, use the DOT-ALL modifier (the trailing s in the following pattern):

    /^((?!hede).)*$/s 

    or use it inline:

    /(?s)^((?!hede).)*$/ 

    (where the /.../ are the regex delimiters, i.e., not part of the pattern)

    If the DOT-ALL modifier is not available, you can mimic the same behavior with the character class [\s\S]:

    /^((?!hede)[\s\S])*$/ 

    Explanation

    A string is just a list of n characters. Before, and after each character, there’s an empty string. So a list of n characters will have n+1 empty strings. Consider the string "ABhedeCD":

        ┌──┬───┬──┬───┬──┬───┬──┬───┬──┬───┬──┬───┬──┬───┬──┬───┬──┐ S = │e1│ A │e2│ B │e3│ h │e4│ e │e5│ d │e6│ e │e7│ C │e8│ D │e9│     └──┴───┴──┴───┴──┴───┴──┴───┴──┴───┴──┴───┴──┴───┴──┴───┴──┘      index    0      1      2      3      4      5      6      7 

    where the e‘s are the empty strings. The regex (?!hede). looks ahead to see if there’s no substring "hede" to be seen, and if that is the case (so something else is seen), then the . (dot) will match any character except a line break. Look-arounds are also called zero-width-assertions because they don’t consume any characters. They only assert/validate something.

    So, in my example, every empty string is first validated to see if there’s no "hede" up ahead, before a character is consumed by the . (dot). The regex (?!hede). will do that only once, so it is wrapped in a group, and repeated zero or more times: ((?!hede).)*. Finally, the start- and end-of-input are anchored to make sure the entire input is consumed: ^((?!hede).)*$

    As you can see, the input "ABhedeCD" will fail because on e3, the regex (?!hede) fails (there is "hede" up ahead!).

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

Sidebar

Related Questions

Know if it's possible to access the iPhone compass in Safari using JavaScript? I
I know how to open a window with openGL (using Win32 or other toolkits).
I know that normally there are no problems using Git from different OSs to
Know of an OCAML/CAML IDE? Especially one that runs on Linux?
Anyone know if it's possible to databind the ScaleX and ScaleY of a render
I know that I can do something like $int = (int)99; //(int) has a
Know of a way to mock %[]? I'm writing tests for code that makes
know nothing about php, but I have this script that reads a folder and
I know, it's possible to define a QObject with custom properties and expose this
I know that we can bind the data to each control within ItemTemplate as

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.