Welcome to the MedHelp forum!
If the pain is on the outside, it can be costochondritis, which causes pain and tenderness at points where your ribs attach to your breastbone. The inflammation around the joint area gives sharp pain on taking a breath. This can also cause tenderness on pressing the area which pains. An orthopedic doctor can diagnose this for you. Generally there is no cause. However it can be due to infection, blow, injury, or fibromyalgia. It usually goes away on its own. Otherwise it can be treated with muscle relaxants, and non steroidal anti inflammatory drugs. It can also be muscle pull. Shingles can also start this way.
If you have pain under your right rib then there could be a liver infection, pancreatitis, or a gall bladder problem. Get a liver function test, ultrasound or CT of abdomen, and pancreatic enzymes. It can also be due to acidity and duodenal ulcer. Take a late night snack. Raise head end of bed. Take Omeprazole empty stomach in morning and an antacid gel at bedtime. Refrain from smoking, alcohol, fuzzy drinks and spicy food. Walking around also helps clear gas. Pain under right ribs with gas can be due to any inflammatory bowel disease like Celiac or Crohn’s, IBS or due to food intolerance. Pain under right rib case can also be due to pleurisy, asthma, or any other lung problems. Usually this is accompanied by lung related symptoms like breathlessness, cough etc. If all these are ruled out, then laparoscopy should be done, especially if you have a previous history of any abdominal or pelvic surgery. Such an undiagnosed pain is usually due to adhesions and cutting the adhesions takes care of the pain. Since a confirmed diagnosis cannot be given on net please discuss with your doctor once. Hope this helps. Take care!
May just be an intercostal neuralgia. Should settle with minor analgesics.