Match everything except my pattern (RegEx)

Emil Mammadov

I tried negative lookahead but not working. I am doing a mistake and I don't know where.

I have a date pattern and want to match everything except that pattern, like below.

Hey how 30.01.2019 are you 23.05.2020 I am fine. ==> (want to match bold areas)

my date pattern is that = ^([0-2][0-9]|(3)[0-1])(\.)(((0)[0-9])|((1)[0-2]))(\.)(\d{4}|\d{2})$

Thanks in advance.

CertainPerformance

You can use a pattern which matches either the date pattern or anything else, up until the start of a date pattern (or the end of the string). Then use a replacer function. If the match is the same format as the date, replace with the entire match (thereby leaving the string unchanged there). Otherwise, replace with whatever you need.

const str = 'Hey how 30.01.2019 are you 23.05.2020 I am fine.';
const datePatternStr = String.raw`(?:[0-2]\d|3[01])\.(?:0\d|1[0-2])\.(?:\d{4}|\d{2})`;
const datePattern = new RegExp(datePatternStr);
const result = str.replace(
  new RegExp(`${datePatternStr}|.+?(?=$|${datePatternStr})`, 'g'),
  (match) => {
    if (datePattern.test(match)) return match;
    return 'foo bar';
  }
);
console.log(result);

The constructed pattern:

${datePatternStr}|.+?(?=$|${datePatternStr})

means:

  • ${datePatternStr} - Match the date pattern, or:
  • .+?(?=$|${datePatternStr}) - Match:
    • .+? One or more characters, as few as possible, up until lookahead matches:
      • $ either the end of the string, or
      • datePatternStr

Note that you can trim your original pattern down to:

(?:[0-2]\d|3[01])\.(?:0\d|1[0-2])\.(?:\d{4}|\d{2})

(only use groups when they're required for the logic you need, \d can be used instead of [0-9], and only use capturing groups when non-capturing groups won't suffice)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Regex to match everything except trailing pattern

Regex - how to match everything except a particular pattern

Match everything except a complex regex pattern and replace it in Pandas

Regex to match everything except this regex

Regex: Match everything except backreference

Regex: match everything but specific pattern

Regex match everything excluding pattern

Match everything except a pattern and replace matched with string

Pattern to match everything except a string of 5 digits

Regex match all except a pattern

Regex, how to select everything except pattern?

Regex that match everything except the list of strings

Regex to match everything except image name and extension

Regex: Match everything except unicode letters

Regex to match everything except strings that starts with digits

python regex, How to match everything except headings?

Regex to match everything outside of a regex pattern

(Nginx) Regex pattern to match everything but www

Regex to match everything until pattern with an exception in it

Match pattern except under one condition Regex

Regex: Match patterns except with pattern preceding

Match everything after particular pattern except whitespace till next letter

Google sheet : REGEXREPLACE match everything except a particular pattern

Regex pattern in Java to replace everything except certain sequence of tokens

How do you regex match everything except the contents of a variable (PHP)

Regex - Match everything except specified characters or split the string

Regex to match everything in a string except "<br>", "<br />", "<br />"

Regex match everything except three consecutive double quotes

Regex correctly matches everything except first match in file