当前位置:编程学习 > JAVA >>

请教提取字符的一个问题~

如有一字符串[(te)-12.4(ch)-12.4(nol)-9(og)23.8(y )-12.1(()5.6(R)-3(E)-9.8(t)-1.2())-6.4( an)-12.4
(d w)9.1(a)-0.3(s)-8.1( th)-12.4(en )]
我只想得到()里的内容也就是
technology (REt) and was then 
主要是括号中的括号应该怎么处理?
请指教 --------------------编程问答-------------------- 有点难 --------------------编程问答-------------------- 一个字符一个字符读取,到达一个左括号,记录一下,找到配对右括号,然后取出中间的值,
如此重复,直到结束。 --------------------编程问答-------------------- 使用ANTLR进行文本解析或者使用正则表达式抽取 --------------------编程问答-------------------- 括号中的括号,有点麻烦。。  --------------------编程问答--------------------
public static void main(String[] args) {
String str = "[(te)-12.4(ch)-12.4(nol)-9(og)23.8(y )-12.1(()5.6(R)-3(E)-9.8(t)-1.2())-6.4( an)-12.4(d w)9.1(a)-0.3(s)-8.1( th)-12.4(en )]";

Pattern p = Pattern.compile("(\\()([\\w\\s\\(\\)]*)(\\))");
Matcher m = p.matcher(str);
while(m.find()){
System.out.print(m.group(2));
}
}
--------------------编程问答--------------------
引用 5 楼  的回复:
Java code

public static void main(String[] args) {
        String str = "[(te)-12.4(ch)-12.4(nol)-9(og)23.8(y )-12.1(()5.6(R)-3(E)-9.8(t)-1.2())-6.4( an)-12.4(d w)9.1(a)-0.3(s)-8.1( th)-12.4(en )]……

太牛了 --------------------编程问答-------------------- +1
引用 5 楼  的回复:
Java code
public static void main(String[] args) {
        String str = "[(te)-12.4(ch)-12.4(nol)-9(og)23.8(y )-12.1(()5.6(R)-3(E)-9.8(t)-1.2())-6.4( an)-12.4(d w)9.1(a)-0.3(s)-8.1( th)-12.4(en )]";……
--------------------编程问答-------------------- 弱弱的问一句,brightyq大神的方法,放在ActionScript里 应该如何写? --------------------编程问答-------------------- http://blog.csdn.net/thirdsmile/article/details/6129752
这个是ActionScript正则表达式的应用方法,正则就用5L给你的,试试咯 --------------------编程问答--------------------
引用 5 楼  的回复:
Java code
public static void main(String[] args) {
        String str = "[(te)-12.4(ch)-12.4(nol)-9(og)23.8(y )-12.1(()5.6(R)-3(E)-9.8(t)-1.2())-6.4( an)-12.4(d w)9.1(a)-0.3(s)-8.1( th)-12.4(en )]";……



+++
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,