2

I need a regular expression that will look up "ship" in any instacne, so: ship, spaceships, starship, shipping etc. However it needs to not look up "warship". Also it needs to be case insensitive. At the moment I've got:

(?!(warship))(?i)ship

...which looks up "ship" but still looks up "warship" thanks to it containing "ship". I've tried:

(?!(warship))^(?i)ship

...which works to an extent but then "starship" doesn't get returned for example. I'm sure the answer is super-simple but I can't see it just now. Your help would be great!

3
  • 1
    What language/tool are you using?
    – anubhava
    Jul 2, 2014 at 11:14
  • does the starship,warship are present in separate lines? Jul 2, 2014 at 11:14
  • 1
    The answer will be vastly different depending on what regex engine you're using, so please tell us which one it is. Jul 2, 2014 at 11:39

4 Answers 4

2

First I wanted to try negative lookbehind:

 /(?<!war)ship/

it should match all words instead of warship. But it gets the ship part only. So it is ok if you just check your string by regexp but doesn't work properly if you want to get the matched word.

3
  • OP wants to match the whole word not only ship. Jul 2, 2014 at 11:39
  • Yes for case insensitive we have to add i modifier. /(?<!war)ship/i. Jul 2, 2014 at 18:02
  • @AvinashRaj Thanks for better solution! Jul 2, 2014 at 18:03
1

I suggest the search string:

(?i)(\w*ships?)(?<!warship)(?<!warships)

(?i) ... enables case-insensitive search.

(\w*ships?) ... matches any string starting with 0 or more word characters, containing ship and optionally also plural s at end in a marking group. Also possible would be (\b\w*ship\w*\b) or (\b[a-z]*ship[a-z]*\b) to find only entire words containing anywhere ship inside.

(?<!warship)(?<!warships) ... two negative lookbehinds checking if the found word is whether warship nor warships.

1
  • This is the best answer so far
    – Aleks
    Jul 2, 2014 at 11:59
1

It appears you may be using the .NET engine or something similarly expressive, so you can use lookbehind.

First you need a regex to match the entire word:

\w*ship\w*

Then you can easily modify it to not match anything where war comes before ship, using negative lookbehind.

\w*(?<!war)ship\w*

Also, there's probably no reason to specify the case insensitivity flag in the regex itself, just apply it to the regex object when you create it.

1

I think you want something like this,

(?i)^(?!warship$)(?=.*ship).*

DEMO

It matches any instances of ship but not a warship

OR

(?i)\b\w*?(?<!war)ship\w*?\b

DEMO

4
  • 1
    Wow, brilliant! I had to add in case insensitivity so it's ended up as: ^(?!(?i)warship$)(?=.*(?i)ship).* but this is now working perfectly. Thank you. Jul 2, 2014 at 11:27
  • @M42 yep, op wants like that. Jul 2, 2014 at 11:34
  • @user3725997 just (?i)^(?!warship$)(?=.*ship).* is enough. Jul 2, 2014 at 11:35
  • @AvinashRaj Sorry, after my initial excitement, M42 was right, it only works if Warship is at the beginning of a string. I need to be able to match warship anywhere in the string. My fault for not clearly explaining that part. Jul 2, 2014 at 11:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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