正则表达式,用于在Java中查找字符串中的数字

时间:2018-07-30 18:08:11

标签: java regex

我正在尝试在字符串中查找数字,但该数字不应位于尖括号内。

eg. String s = "214投資者、43発行者<w1><\w1>";
ans : 214 and 43 not 1 because it is inside <>

我需要找到不在<>内的字符串中的数字。字符串始终保持平衡<>,后跟<\>

我的代码能够找到数字,并且可以排除<>中的数字。但是,如果我添加了</w1>,它将产生错误。     导入java.util.regex.Matcher;     导入java.util.regex.Pattern;

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World");

        String s = "214投資者、43発行者<w99><\w99>";
        Pattern p = Pattern.compile("[0-9]+(?![^<]*>)") ;
        Matcher m = p.matcher(s);
        int index = 0;
        StringBuffer str = new StringBuffer();
        while (m.find()) {
            String match = m.group();
            System.out.println(match);
        }
    }
}

0 个答案:

没有答案