Can Pac-Man Eat This String? In the arcade version of the game, Pac-Man eats pac-dots. However, in this challenge, he's hungry for alphanumeric characters and punctuation in a string.

Your task is to create a function that feeds Pac-Man a string, evaluates if he can eat it or not, and returns the string with Pac-Man's location in it.

Pac-Man (&lt;) eats characters from left to right, leaving an underscore or space for each character as he goes, and his goal is to get from the first position-1 to the last position+1:

1. &lt;Pac
2. _&lt;ac
3. __&lt;c
4. ___&lt;


However, Pac-Man's natural enemy, the ghost, will stop him if he encounters one of the letters in the word "GHOST" (not case sensitive). Your function should return the string with Pac-Man's location as he encounters the ghost character:

1. &lt;No!
2. _&lt;o!


The only thing that can defeat a ghost is a power pellet. If Pac-Man reaches a letter in the word "PELLET" (also not case sensitive) before coming to a ghost, he will eat the ghost and keep moving, and that pellet will be used up. Power pellets can stack (ie, in ppgg both ghosts would be eaten). The T character exists as both as a ghost and a pellet, so it can be ignored (treated as any other letter, like a).

1. &lt;Pop
2. _&lt;op
3. __&lt;p
4. ___&lt;


To further clarify, in the string "Pac-Man loses here", the following operations occur:

P &lt;P, +1 Pellet (1 pellet)
a &lt;a
c &lt;c
- &lt;-
M &lt;M
a &lt;a
n &lt;n
  &lt;[space]
l &lt;l, +1 Pellet (2 pellets)
o &lt;o, -1 Pellet (1 pellet)
s &lt;s, -1 Pellet (0 pellets)
e &lt;e, +1 Pellet (1 pellet)
s &lt;s, -1 Pellet (0 pellets)
  &lt;[space]
h &lt;h, ghost wins, returns
e
r
e


Examples

Input: Pacman wins!
Output: ____________&lt;

Input: Pacman loses wah-wah :(
Output: _______________&lt;h-wah :(

Input: PELLET PELLET GHOST
Output: ___________________&lt;

Input: Hello World!
Output: &lt;Hello World!

Input: &lt;_!@12&lt;_&lt;_&lt;
Output: ___________&lt;


This is code-golf--lowest score in bytes wins.
Retina, 55 38 bytes

i`^(([elp])|[^ghos]|(?&lt;-2&gt;.))*
$.&amp;$*_&lt;


Try it online! (The first line just allows running several test cases at once.)

Explanation

The problem is essentially to find the longest prefix that doesn't have an unmatched closing parenthesis. Except that we can use either e, l or p in place of ( and either g, h, o or s in place of ).

Hence, this solution is almost a textbook example of balancing groups. I won't go into too much detail about how they work, as this code is essentially the same as the standard example you can read up on in my SO answer on balancing groups.

The entire program is therefore a single regex substitution. The i activates case-insensitivity. Then we either match a pellet with [elp] and increment the depth counter (in the form of the capture stack of group 2), or we match something that isn't a ghost with [ghos] or we match a ghost with . and decrement the depth counter by popping from stack 2. Of course, in principle this allows matching a pellet with the [^ghos] section or a non-ghost with the . section, but thanks to greedy matching and the way the regex is backtracked, these possibilities are never attempted by the regex engine.

The substitution then uses two Retina specific features: $* repeats the character to its right as many times as specified by the token on its left. That token is $.&amp; which is the length of the entire match. This just means that we replace each character in the match with a _. And then we also append a &lt; to those underscores. The part of the input that isn't eaten simply remains unaffected by the substitution.