How can i invert my Regex matching results?

I have checked a lot of information about how can i invert my match..but unfortunatelly i coundnt be able to do it in for my problem. So..lets say i have this text :

GS Sos_519 082_KO_Ge124222sshelden892 345

My goal is to match eveything that is not XYZ XYZ(numbers only). So i would like my results to be: 519 082892 345. Right now i've managed to do the opposite using this regex: \d\d\d\s\d\d\d. It matches exactly 519 082892 345 and now i have to invert it. I went to the logic to match what i want as results and then invert it..thinking it would be easyer that way..but i might be wrong. Matching everthing that is not in that specific format looks really complicated. I've tryied to invert it using regex like this :

^(?:[\d\d\d\s\d\d\d].)*$

but unfortunately it does not match anything. I am new in regexes and i would really appreciate some help. Thank you in advance !

2

1 Answer

The strategy, here, is to try to match 3 digits spaces 3 digits (i.e. \d{3}\h+\d{3}) and doesn't take care of them (i.e. don't remove), or, match any character and remove it.

The lookarounds (?<!\d) and (?!\d) is used to make sure we don't match 4 digit numbers.

  • Ctrl+H
  • Find what: (?<!\d)\d{3}\h+\d{3}(?!\d)(*SKIP)(*FAIL)|.
  • Replace with: LEAVE EMPTY
  • CHECK Wrap around
  • CHECK Regular expression
  • UNCHECK . matches newline
  • Replace all

Explanation:

(?<!\d) # negative lookbehind, make sure we haven't digit before
\d{3} # 3 digits
\h+ # 1 or more horizontal spaces
\d{3} # 3 digits
(?!\d) # negative lookahead, make sure we haven't digit after
(*SKIP) # skip this match
(*FAIL) # force the match to fail | # OR
. # any character but newline

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like