0

I have a regular expression to check if a given string does not contain "blog" prefix:

^(?!blog)

Unfortunately, I can't use look-ahead syntax in Go Lang :(

How to rewrite that reg expression to remove ?! look-ahead syntax?

Simon
  • 1,099
  • 1
  • 11
  • 29
  • `^(?!blog)` matches a string that does not start with `blog`, not that does not contain it. What is your real problem? – Wiktor Stribiżew Jun 07 '20 at 17:26
  • Sorry, maybe I wasn't clear. I need to rewrite that rexex to match all strings without `blog` prefix and not use `?!` look-ahead syntax – Simon Jun 07 '20 at 17:29
  • Is it the whole regex, or is it part of a larger regex? – Wiktor Stribiżew Jun 07 '20 at 17:30
  • It's the whole regex – Simon Jun 07 '20 at 17:32
  • I would think that `!str.begin_with?("blog")` could be implemented with a line or two of code in every general-purpose language. Is there a reason you want or need to use a regex for that, or are you just curious, which would be fine? – Cary Swoveland Jun 07 '20 at 17:37
  • 1
    `^(([^b].{3}|.[^l].{2}|.{2}[^o].|.{3}[^g]).*|.{0,3})$`, [demo](https://regex101.com/r/9zgEUR/2). See [Regex: match everything but specific pattern](https://stackoverflow.com/a/37988661/3832970), scroll to *string **starting with** a specific pattern* – Wiktor Stribiżew Jun 07 '20 at 17:44
  • If the first four characters of the string are known to be lowercase letters you could use an alternation: `^(?:aaaa|aaab|...|zzzy|zzzz)` with the exception of `|blog|`. There are only 456975 terms in the alternation, so available memory would not impose a limitation. – Cary Swoveland Jun 07 '20 at 17:44
  • Good one, @Wiktor! – Cary Swoveland Jun 07 '20 at 17:45
  • Readers: this question was closed because it is a duplicate of an earlier question. The cited earlier question is somewhat different as it has no requirement that lookarounds cannot be used, a central element of this question. In fact, the selected answer to the earlier question uses a lookaround. However, part of one answer (@Wiktor's) gives a solution for "regex engines not supporting lookarounds", which applies here. – Cary Swoveland Jun 07 '20 at 18:26

0 Answers0