Skip to content
Fix Code Error

How to negate specific word in regex?

March 13, 2021 by Code Error
Posted By: Anonymous

I know that I can negate group of chars as in [^bar] but I need a regular expression where negation applies to the specific word – so in my example how do I negate an actual bar, and not “any chars in bar”?

Solution

A great way to do this is to use negative lookahead:

^(?!.*bar).*$

The negative lookahead construct is the pair of parentheses, with the opening parenthesis followed by a question mark and an exclamation point. Inside the lookahead [is any regex pattern].

Answered By: Anonymous

Related Articles

  • Reference - What does this regex mean?
  • Is it possible to apply CSS to half of a character?
  • Fastest way to iterate over all the chars in a String
  • Baffling variadic templates exercise
  • What does this symbol mean in JavaScript?
  • How to use Regular Expressions (Regex) in Microsoft…
  • How do i calculate total without manually inputting it?
  • What is your most productive shortcut with Vim?
  • javascript .replace and .trim not working in vuejs
  • Unexpected behaviour java priority queue. Object…
  • What are the undocumented features and limitations…
  • shell script. how to extract string using regular…
  • Regex to match only uppercase "words" with some exceptions
  • What is C# analog of C++ std::pair?
  • Is there a regular expression to detect a valid…
  • What is a non-capturing group in regular expressions?
  • C# options for dealing with non-void methods that…
  • PHP: How to remove all non printable characters in a string?
  • How to calculate Cohen's D across 50 points in R
  • How to check if a string contains only digits in Java
  • VueJs Url Rewrite Error when hosting on IIS with…
  • How to capture a word boundary in Swift NSRegularExpression?
  • Polymer 1.0 Trying to make a splitter which works…
  • How to make Javascript font responsive?
  • Regex lookahead, lookbehind and atomic groups
  • How to split a string in Java
  • Ukkonen's suffix tree algorithm in plain English
  • Java Regex Capturing Groups
  • How to negate the whole regex?
  • java.util.regex - importance of Pattern.compile()?
  • SQL Query for Student mark functionality
  • Does Java SE 8 have Pairs or Tuples?
  • Maintaining a PriorityQueue of Pairs such that it's…
  • Spark EMR job jackson error -…
  • Regex Named Groups in Java
  • Permutations of the same 4 digits resulting into 2 pairs
  • complexity of removing the duplicate element from a…
  • Simple logical operators in Bash
  • How to validate an email address using a regular expression?
  • Java - Regex, nested recursion match
  • Regex negative lookahead is not working properly
  • Bootstrap Card - change width
  • without template, need to render not return json
  • Is there some way to test my grammar which refer to…
  • What does the red exclamation point icon in Eclipse mean?
  • What's the difference between eval, exec, and compile?
  • How to set width of mat-table column in angular?
  • Aligning equations with multiple marks and lineups…
  • What does an exclamation mark mean in the Swift language?
  • Regex to find all placeholder occurrences in text
  • Python regex expression between variable strings and…
  • JOI how to allow empty spaces and punctuation
  • Adding a unique id to appended forms in Jquery or Javascript
  • TypeError: argument of type 'NoneType' is not iterable
  • Fix top buttons on scroll of list below
  • Regex pattern to match all, and return null when…
  • Regular Expression for any number greater than 0?
  • How to use if-else logic in Java 8 stream forEach
  • Java - Values are added multiple times in a list
  • How do I sort an observable collection?
  • How can I parse a CSV string with JavaScript, which…
  • Regex that matches newlines literally and passively
  • Length of string in bash
  • Inserting the succeeded word before 'and'…
  • Swift extract regex matches
  • Binding between two arbitrary objects in Ember.js
  • How do you match strings in custom Instruments…
  • RegEx match open tags except XHTML self-contained tags
  • How to enforce required paper-radio-group in Polymer?
  • Get price (amount only, no currency tag, no special…
  • Filter by field on relationship in SQLAlchemy
  • how to use value of a column as input to a spatial operation
  • What makes the overload fail between these two…
  • Regular expression to match numbers with or without…
  • Difference between / and /* in servlet mapping url pattern
  • Form field border-radius is not working only on the…
  • Regex matching in a Bash if statement
  • exclude some characters from being able to write…
  • How to match an optional entire phone number using…
  • Why does this regular expression not work in Vim…
  • How to speed up for loop in R
  • With a single variable that is a string, perform an…
  • What causes this bug and how can I fix it?
  • Named regular expression group "(?Pregexp)": what…
  • How to grep Git commit diffs or contents for a certain word?
  • Regular expression pattern matching in JavaScript?
  • Passing string into a lambda function
  • Regular expression for floating point numbers
  • Removing a list of characters in string
  • What is the => assignment in C# in a property signature
  • How do you parse and process HTML/XML in PHP?
  • Creating an dynamic array, but getting segmentation…
  • How to reuse function with parameters in Vue…
  • Regex: Ignore order of capture groups and capture…
  • c++ custom compare function for std::sort()
  • Regular Expression to match every new line character…
  • Hidden property of a button in HTML
  • Can I construct base class to allocate while…
  • Regex: capture function with arguments in a…
  • Sorting 1 million 8-decimal-digit numbers with 1 MB of RAM

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

OnChange event handler for radio button (INPUT type=”radio”) doesn’t work as one value

Next Post:

How to comment multiple lines in Visual Studio Code?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error