Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

java - Pattern to extract text between parenthesis

How to extract string from "(" and ")" using pattern matching or anything. For example if the text is

"Hello (Java)"

Then how to get only "Java"?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try this:

String x = "Hello (Java)";
Matcher m = Pattern.compile("\((.*?)\)").matcher(x);
while (m.find()) {
    System.out.println(m.group(1));
}

or

String str = "Hello (Java)";
String answer = str.substring(str.indexOf("(")+1, str.indexOf(")"));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...