1

In continuation of a previous question I had:

So I'm in a situation where I MUST use only regex to select everything but a specific word. For the purposes of example, the word will be 'foobar'. This is what should happen:

The original regex that works with this example is /\W*\b(?!foobar).+?\b\W*/g.

match_highlighted.png

The problem with that regex is that things like foobartest and foobar1234 with anything after foobar, is ignored along with the foobar. This is not what I want. The test and 1234 should not be getting ignored, however the starting foobar should, it shouldn't disclude the entire word because it wasn't exactly foobar. The only scenario where this should occur is with something like foobarfoobar, where it ignores both foobars as intended. Everything else should be matched without exception.

The supposed duplicate does not accomplish what I'm requesting.

One last thing to note is that I can not use lookbehind, only lookahead. Is there any way to achieve what I want? Thanks!

Community
  • 1
  • 1
Izzy
  • 272
  • 1
  • 14
  • What about it doesn't work? – Anonymous Aug 08 '15 at 01:48
  • @Anonymous this is giving me an "infinite" error. To help, I'm testing my regex on the site http://regexr.com/ – Izzy Aug 08 '15 at 02:00
  • It shouldn't give an infinite error if used exactly like that, but you're right, there is a problem with it. I can post an answer, but I would need to know what language you're using first. Sorry about the misunderstandings, my bad. – Anonymous Aug 08 '15 at 02:03
  • @Anonymous I'm using a javascript-based regex engine. Regexr is the best sandbox I've found for the type of regex I'm able to use. – Izzy Aug 08 '15 at 02:05
  • Also, why must you use regex? I can imagine much better ways to do this. – Anonymous Aug 08 '15 at 02:10
  • It's for a specific purpose, trust me. I **must** use regex, just assume there are no alternatives. – Izzy Aug 08 '15 at 02:12
  • @Flipybitz Please don't use images. Please. That is code you could have formatted. – Ely Aug 08 '15 at 02:15
  • To be honest I don't think it is possible with your restrictions, I mean you can see if a word starts with foobar but you can't jump 6 characters ahead then... – maraca Aug 08 '15 at 02:18
  • @Elyasin if it upsets you because you can't copy and paste it, get the original from my first post http://stackoverflow.com/questions/31808068/regex-select-all-but-group – Izzy Aug 08 '15 at 02:18
  • maraca is right. It isn't possible with regular expressions as restricted as JavaScript's are (at least to my knowledge). Some regular expression languages could handle it, but it would best be done through some other means. – Anonymous Aug 08 '15 at 02:20
  • 1
    @Anonymous so if it's impossible, can someone post an answer explaining how it is? I'll accept that response as the answer in that case. – Izzy Aug 08 '15 at 02:24

1 Answers1

1

JavaScript regular expressions simply don't allow this functionality. Less restrictive languages allow the ability to drop matches and continue to attempt to match the regex. However, JavaScript does not allow this. Therefore, it will never skip over characters. It attempts to match every single one. It would then still, theoretically, be possible in a convoluted way if lookbehinds were supported, but they are not either.

There is not a foolproof way without these two possibilities to assure that a regular expression is not currently attempting to match in the middle of a particular string of characters.

Anonymous
  • 11,748
  • 6
  • 35
  • 57