1

What I want to do is select everything except [0-9]{4,7}. I know negative lookaheads so I tried this: (?![0-9]{4,7}) but the result was strange. Any ideas to get this?

For example,

Sample text for my question!
2020 0316 or 200316
fdsa2020asdf

Desired

Sample text for my question!
  or 
fdsaasdf
Bohemian
  • 365,064
  • 84
  • 522
  • 658
stari
  • 51
  • 1
  • 7
  • Does this answer your question? https://stackoverflow.com/questions/1687620/regex-match-everything-but-specific-pattern – Tomas Zubiri Mar 16 '20 at 03:59
  • Does this answer your question? [Regex: match everything but specific pattern](https://stackoverflow.com/questions/1687620/regex-match-everything-but-specific-pattern) – Tomas Zubiri Mar 16 '20 at 03:59
  • 1
    I suggest you add "123" and "12345678" to your example. Which regex engine should be used? PCRE (PHP)? – Cary Swoveland Mar 16 '20 at 04:01
  • 2
    AFAIK, this is not possible using standard regular expressions. However, it may be possible in particular dialects. Per [tag:regex] tag rules, "Since regular expressions are not fully standardized, all questions with this tag should also include a tag specifying the applicable programming language or tool." – Amadan Mar 16 '20 at 04:06
  • @Amadan You're correct, however I can't know which one of the "REGEXes" I'm using. I'm working on an app named KLCK, and it doesn't saying me anything about the engine – stari Mar 16 '20 at 07:37
  • @이준모 Do not edit a question to become a different question, because it invalidates all comments and answers. I have rolled back your recent edit that fundamentally changed the question. Instead, leave this question as it is and ask a *new* question. – Bohemian Mar 16 '20 at 07:42
  • @Bohemian I thought it was not changing its fundamental, because I thought it was difference of explaining. Rollbacked version is displaying strings that _should be matched_ so that I could just replace them with empty string. Edited version is displaying strings of _results_. i'm a real noob, so if it was inappropriate I apologize. – stari Mar 16 '20 at 07:50
  • @stari You asked *I want to do is select everything except `[0-9]{4,7}`*. People answered that question. If you change the question (the changed version was effectively the opposite of the original) to something different, then you invalidate answers, and besides future visitors may actually want an answer to the original question. If you realise that you asked the wrong question, it's not OK to "fix your mistake" by changing the question into something new. But it is completely OK to ask a *different* question as a *new* question, which is what you should do. – Bohemian Mar 16 '20 at 20:28

1 Answers1

0

This matches the desired parts of all your test cases:

\D{2,}

See live demo.

I added {2,} only to avoid matching the space between 2020 and 0316. If you're ok with matching that, you can remove {2,}.

Bohemian
  • 365,064
  • 84
  • 522
  • 658