AloneInTheDark
AloneInTheDark

Reputation: 938

Java string split with Multiple Characters Delimiter

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?");  

Upvotes: 0

Views: 163

Answers (3)

Sabuj Hassan
Sabuj Hassan

Reputation: 39443

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_", "");

Upvotes: 2

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62874

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

For example:

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

Upvotes: 1

Keppil
Keppil

Reputation: 46239

You could just do

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

Upvotes: 2

Related Questions