-1

In question, I need a Regex to accept everything except for specific patterns of strings. I've tried using negative lookahead examples from this Stackoverflow Question Forum:

Regex: match everything but specific pattern

But it looks that the patterns of words are different than a pattern of characters.

The patterns I need to exclude are the ones listed below:

Character patterns to exclude

Robin
  • 15
  • 1
  • 7
  • 1
    So, *how* did you use them? *Where*? – Wiktor Stribiżew Nov 29 '21 at 22:19
  • I tried using them like this: `^(?!.*:[^\\])$` but it doesn't seem to work either. – Robin Nov 29 '21 at 22:27
  • Why did you glue the `$` anchor? There is no example like this at the page you linked to. There is a clear example, like `^(?!.*patternToExclude)`. If you have more patterns, simply use `^(?!.*patternToExclude)(?!.*patternToExclude2)(?!.*patternToExclude3)`. Besides, if you use `"^(?!.*:[^\\])"` in Java or `"~^(?!.*:[^\\])~"` in PHP, you will get an exception. – Wiktor Stribiżew Nov 29 '21 at 22:28
  • Those are string patterns, the `/` character for example, can't be excluded like that, because it's marked as a pattern error, the `$` is only to declare the end of the regex. – Robin Nov 29 '21 at 22:32
  • `$` does not declare regex end. You never need any markers for that. `$` requires the end of string position. – Wiktor Stribiżew Nov 29 '21 at 22:33
  • Ok I understand that, but this `^(?!.*://)` doesn't work either, that's what I'm trying to exclude.. – Robin Nov 29 '21 at 22:35
  • No, [it works well](https://regex101.com/r/rlZVu9/1). You are just using it in a wrong way. But we do not know how. – Wiktor Stribiżew Nov 29 '21 at 22:37
  • So, which is the right way? I wouldn't be here asking for help if I knew the answer... – Robin Nov 29 '21 at 22:39
  • We can't help you because you are not revealing ***how and where*** you are using the regexps. Add the relevant code to the question. – Wiktor Stribiżew Nov 29 '21 at 22:45
  • Maybe you're using javascript, in which case you need to escape the `//` with `\/\/`? But as Wiktor says, you haven't given us any context for how you're using them so it makes it difficult for us to imagine what is going wrong; if you provide a snippet of code with the exact code which is failing then we might be better placed to help you :) – Dominic Price Nov 29 '21 at 22:47
  • I need to use the regex to detect when those patterns are used on an input box, so I can show an error message. There's no code failing yet, because I'm trying to build the regex at regex101.com but it says pattern error. – Robin Nov 29 '21 at 22:49
  • regex101 usually gives you very detailed information about what the error is; can you copy the whole output for us? – Dominic Price Nov 29 '21 at 23:02
  • Sure: `All the errors detected are listed below, from left to right, as they appear in the pattern. / An unescaped delimiter must be escaped with a backslash (\) / An unescaped delimiter must be escaped with a backslash (\)` – Robin Nov 29 '21 at 23:20

1 Answers1

0

This is the correct way of excluding the patterns:

^(?!.*:\/\/)(?!.*\.\.\/)(?!.*:\\)$
Robin
  • 15
  • 1
  • 7
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 30 '21 at 01:28