I've been trying to get a specific regex working but I can't get it to do what I need.

Basically, I want it to look for ROCKET. The regex should match ROCKET in upper or lower cases, and with or without punctuation, but not when part of another word. So, the regex would trigger on any of these:

hi Rocket
This is a rocket.
ROCKET's engine

but NOT trigger on ROCKET when it is found in something like


I've been trying to get it right using a regex generator online but I can't get it to match exactly.

  • 1
    This is one of those [infrequent] situations where the question might be better suited for Stack Overflow. Be sure to provide a language and/or platform as each language has its own peculiarities. For example, Windows. .Net and the Regex class. (Usually, its the other way around. Stack Overflow gets hundreds of off-topic questions from developers that are better suited for Super User). – jww Apr 18 '15 at 22:14

I suggest bookmarking the MSDN Regular Expression Quick Reference

you want to achieve a case insensitive match for the word "rocket" surrounded by non-alphanumeric characters. A regex that would work would be:


What it will do is look for zero or more (*) non-alphanumeric (\W) characters, followed by a case insensitive version of rocket ( (?i)rocket(?-i) ), followed again by zero or more (*) non-alphanumeric characters (\W). The extra parentheses around the rocket-matching term assigns the match to a separate group. The word rocket will thus be in match group 1.

UPDATE 1: Matt said in the comment that this regex is to be used in python. Python has a slightly different syntax. To achieve the same result in python, use this regex and pass the re.IGNORECASE option to the compile or match function.


On Regex101 this can be simulated by entering "i" in the textbox next to the regex input.

UPDATE 2 Ismael has mentioned, that the regex is not quite correct, as it might match "1rocket1". He posted a much better solution, namely


  • 1
    Testing this out with regex testers online (regex101.com for example) shows it as invalid and not matching example strings that I enter. This is intended to be used as part of a python script. Does that make any difference in how it should be written? – Kefka Apr 18 '15 at 19:55
  • 1
    yes it does. you can see on regex101.com that you can choose a regex "flavour" on the top left, python is slightly different. I'll update my answer with the python equivalent. – Xaser Apr 18 '15 at 20:31
  • 1
    Thanks. I thought regexes were basically language independent. – Kefka Apr 18 '15 at 20:33
  • 1
    They ought to be, but minor implementation differences exist. – Xaser Apr 18 '15 at 20:36
  • 2
    And \W*(rocket)\W* matches lrocketl. It should be (?:^|\W)(rocket)(?:$|\W) (without the * and you have to check if it matches the start and/or end of the string). – Ismael Miguel Apr 18 '15 at 21:33

I think the look-aheads are overkill in this case, and you would be better off using word boundaries with the ignorecase option,


In other words, in python:

>>> x="rocket's"
>>> y="rocket1."
>>> c=re.compile(r"\brocket\b",re.I)  # with the ignorecase option
>>> c.findall(y)
>>> c.findall(x)
  • technically, non-capturing groups are no lookarounds, however the /b option yields the exact same result as Ismael's solution, but may be a little more elegant. – Xaser Apr 19 '15 at 10:28

With grep and sed, you can use \<rocket\>.  With grep, the -i option will make it case-insensitive (ignore case):

grep -i '\<rocket\>'

I don't know any way to make all sed regexes case-insensitive, but there's always the caveman way:

sed -n '/\<[Rr][Oo][Cc][Kk][Ee][Tt]\>/p'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.