-1

Is there a way to match a word not containing a palindrome (be it as long as it may)?

For instance, for a 6-character-long palindrome, foo/bar would match but xbarrabzz/1xoxxoxa14 would not match.

user3942918
  • 25,539
  • 11
  • 55
  • 67
Guy Waldman
  • 457
  • 3
  • 12
  • Can you give one more example? – Harsh Aug 19 '16 at 02:51
  • Yes, of course. (for polynomial of length 2) Match: hello, world, sun, x, mine, hola Non-match: ohho, izzil, 112maam, 01221555 – Guy Waldman Aug 19 '16 at 02:56
  • 2
    I think you mean *palindrome* (not "polynomial") – Bohemian Aug 19 '16 at 02:58
  • @Bohemian Amazingly embarassing. It's late :( – Guy Waldman Aug 19 '16 at 03:02
  • Would `xyx` be considered a palindrome? – Aran-Fey Aug 19 '16 at 03:23
  • @Rawing It would technically, yes. In this case, I don't really care about an optional middle character inside the palindrome, but wanted to know the technique in JS Regex. – Guy Waldman Aug 19 '16 at 03:26
  • The esasiest way to check if a string is a palindrom is often to reverse it and then compare the reversed version with the original. "contains" is kind of ambiguous, – Jasen Aug 19 '16 at 03:36
  • requirements seem ambiguous. `racecar` is a palindrome but do you consider `a toyota race car` to contain palindromes ? – Jasen Aug 19 '16 at 03:38
  • If the regex is looking for 1 captured character and an optional "middle" character, `oyo` would be considered a palindrome. So in the phrase `a toyota race car`, the word `toyota` would match that palindrome. Therefore, the only word that would NOT contain a palindrome is `toyota` and it would not be matched. I am looking for words, basically, but not exclusively alpha-numeric. That's why I said phrases. – Guy Waldman Aug 19 '16 at 03:46

2 Answers2

2

Use a negative lookahead, for example for length 5/6 (3-letter with middle letter reused or doubled):

^(?:(.)(?!(.)(.)\3?\2\1))*$

See live demo.

But you would have to add another look ahead for each length (which I leave as an exercise for the reader).

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Still no, sorry. I tried using a negative lookahead on my own to no avail. – Guy Waldman Aug 19 '16 at 03:09
  • I've made another change, and tested it and it seems to work. But this is for 2.5 and 3 letter words only as an example. You would have to add in the others to a reasonable maximum length. – Bohemian Aug 19 '16 at 03:14
  • Right. Sorry, your solution is correct (and very much appreciated!). I was aiming for Javascript regex and of course it wouldn't work on regex101 and on my tests because it doesn't support negative lookahead). Would you know of any JS solution? – Guy Waldman Aug 19 '16 at 03:21
  • @guy I don't think it can be done without a negative look ahead, because you can't use a back reference inside a negated character class, ie `[^\1]` doesn't work, and you need a way to disallow chars captured as a single-char group in specific positions. – Bohemian Aug 19 '16 at 06:07
1

You can use \b(?:(?!(\w)(\w)\2?\1)\w)+\b.

Online Demo.

It's a simple negative lookahead that checks if the word contains a structure like xyx or xyyx.

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
  • Fantastic. Thank you. I was under the impression that JS did not support negative lookahead for some reason (as asked [here](http://stackoverflow.com/questions/6851921/negative-lookahead-regular-expression)) and that was why Bohemian's solution did not work. Apparently not. – Guy Waldman Aug 19 '16 at 03:50
  • @GuyWaldman It probably didn't work because of the start-of-string and end-of-string anchors `^` and `$`. – Aran-Fey Aug 19 '16 at 03:52