0

I'm usually pretty good with regex but this one has me stumped. I've read the other SO posts about negative looks but nothing seemed to work. I'm looking for a pure regex solution (not piped through anything like grep) because this has to be a single operation (in Atom IDE search).

Here's the issue:

Looking for all instances of the word "core" that are NOT preceded by "vpc_" and specifically capturing "core", not lines that contain core, because this is for Find/Replace, so I don't want to "select" the whole line, just the word core.

EDIT:

resource "aws_internet_gateway" "igw_core" {
    vpc_id = "${aws_vpc.vpc_core.id}"

    tags {
        Environment = "${var.environment}"
        Name        = "${var.environment}_igw_core"
        Version     = "${var.version}"
    }
}

I want to replace "core" with "foo" except "vpc_core" which should remain. The result should be:

resource "aws_internet_gateway" "igw_foo" {
    vpc_id = "${aws_vpc.vpc_core.id}"

    tags {
        Environment = "${var.environment}"
        Name        = "${var.environment}_igw_foo"
        Version     = "${var.version}"
    }
}
Spanky
  • 5,608
  • 10
  • 39
  • 45
  • 1
    Probably something like `(?<!vpc_)core` would work. I don't have a copy of atom around to test but that should match any `core` without `vpc_` immediately preceding it. Seems to work fine for me in sublime search. – Jonathan Kuhn Jun 22 '17 at 20:41
  • Can you give us a few examples what should be matched and what not – techfly Jun 22 '17 at 20:42
  • 1
    Maybe just `\bcore\b`? `_` is a word char, and the word boundary will eliminate the possibility of matching `vpc_` before it. – Wiktor Stribiżew Jun 22 '17 at 20:43
  • I guess javascript doesn't support lookbehinds so scratch my previous comment. Without more use cases, I would say that wiktor's comment is the closest you will get. – Jonathan Kuhn Jun 22 '17 at 20:47
  • I think this is an X/Y problem. Could you give an example of text in which you want to do search and replace operation, and explain how you would like it changed? – Sergey Kalinichenko Jun 22 '17 at 20:49
  • @WiktorStribiżew This is almost certainly not what OP is looking for, because of the prefix that he mentions, but on the other hand I wish he provided a little more info on what he needs to achieve. – Sergey Kalinichenko Jun 22 '17 at 20:50
  • 1
    @dasblinkenlight This is almost certainly what should work given the current requirements. – Wiktor Stribiżew Jun 22 '17 at 20:52
  • Hence the sentence *I'm usually pretty good with regex...*? @WiktorStribiżew – revo Jun 22 '17 at 20:54
  • @WiktorStribiżew Not really, because OP wants to ignore `vpc_core`, but presumably not `somethingelse_core`. Regex `"\bcore\b"` will ignore both. – Sergey Kalinichenko Jun 22 '17 at 20:54
  • @revo just because someone is "pretty good with regex" doesn't mean they didn't miss something obvious. Especially if they started with more complicated solutions like lookahead/behind. – Jonathan Kuhn Jun 22 '17 at 20:55
  • @dasblinkenlight but they said they wanted to match the word `core`, not `score`, `somethingelse_core`...etc. Given the current requirements, his comment works as described. – Jonathan Kuhn Jun 22 '17 at 20:56
  • Thank you for opening up a whole new world to me! @JonathanKuhn – revo Jun 22 '17 at 20:57
  • @dasblinkenlight: This is an open community. If anyone thinks the question is great and can and should be answered another way, cast re-open votes. – Wiktor Stribiżew Jun 22 '17 at 20:58
  • @revo I only say that because given my first comment I went straight to a lookbehind without even thinking of the simpler word boundary. OP "poisoned the well" on that one by suggesting it and so i thought to help them with what they mentioned. Same thing happens all the time. And I would consider myself "pretty good with regex". – Jonathan Kuhn Jun 22 '17 at 21:00
  • Actually you didn't get the irony, or you wouldn't have gone for any details. @JonathanKuhn – revo Jun 22 '17 at 21:10
  • @WiktorStribiżew: This is not the exact duplicate of another question, and if it is please feel free to share a link. Similar questions have been asked but I feel this question is nuanced enough to stand alone (or I wouldn't have asked it and created the noise) – Spanky Jun 22 '17 at 22:50
  • And yes, I am "usually pretty good with regex" but this question stumped me, which is why I asked. If I could've figured it out myself, I wouldn't waste my time or yours asking. I've written successful RFC822 regex's before, this one just gave me some challenges that I couldn't solve myself. Thanks to everyone who chimed in. – Spanky Jun 22 '17 at 22:56
  • Ok, [`(?<!\bvps_)core\b`](https://regex101.com/r/QglWhD/1) should be enough. – Wiktor Stribiżew Jun 22 '17 at 23:01

0 Answers0