0

I have a very complicated output from a function, which i need to use specific word from it.

For example, my output is:

 oracle   11257     1  0 14:01 ?        00:00:00 ora_pmon_sas

I need to get just "sas" word, which is next to "ora_pmon_"

Another example:

oracle   6187     1  0 13:41 ?        00:00:00 ora_pmon_db2

I need to get "db2". So what should be my expression?

JAVA code:

insArray=line.split("what will be between these quotes?");  
3
  • 1
    why are you splitting it if that pattern occurs only once!..does it have multiple such patterns..Please be very specific with your question.. Commented Feb 7, 2014 at 12:21
  • input.substring(input.lastIndexOf('ora_pmon_') + 1) will do the trick... Commented Feb 7, 2014 at 12:23
  • well, there may be another word like "asm_pmon_" instead of "ora_pmon_". But the rest of thing is similar. Commented Feb 7, 2014 at 12:36

3 Answers 3

2

How about this one?

string = string.replaceAll(".*?ora_pmon_", "");

If you want multiple words in place of ora, then it will be

string = string.replaceAll(".*?(ora|kf|asm)_pmon_", "");
Sign up to request clarification or add additional context in comments.

3 Comments

that could be useful but "ora" can be anything like "kf" or "asm". so what's your answer to that case?
replace ora with (ora|kf|asm) Just add your words there with a pipe in between.
but i only need to find them. if there is no words like "ora_pmon_sas", it should pass it.
1

You could just do

String sub = s.substring(s.indexOf("ora_pmon_") + 9);

Comments

0

You can simply use String#substring(int i) combined with String#lastIndexOf(char ch)

For example:

String result = input.substring(input.lastIndexOf('_') + 1)

2 Comments

What about ora_pmon_db2_abc ?? I think better sol may be lastIndexOf('ora_pmon_') as per OP question...
You're right, but the OP hasn't noted that there will be such case.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.