简体   繁体   中英

Regex problem in Java in code sample

I have job with regex in my expressions: example !(FA1_A.i & FA1_M.i)

I have operators: ! ( ) & | ! ( ) & |

The operands have names: [a-zA-Z_]*\\.[a-zA-Z_]

I wrote Java code to split on tokens, but it doesn't split on operators and operands. It should be !, (, FA1_A.i, &, FA1_m.i, ) . Can anybody tell me what is wrong?

String stringOpеrator = "([!|&()])";
String stringOperand = "(([a-zA-Z_]*)\\.([a-zA-Z_]*))";
String reg=stringOpеrator+"|"+stringOperand;
Pattern pattern = Pattern.compile(reg);

Matcher m = pattern.matcher(expression);
// System.out.println("func: " + function + " item: " + item);
while (m.find()) {
    int a=m.start();
    int b=m.end();
    String test=expression.substring(m.start(), m.end());
    String g=test;
    tokens.add(new Token(expression.substring(m.start() , m.end())));


            //m = pattern.matcher(expression);

        }

您提供的示例中的名称似乎包含数字,您的正则表达式不匹配。

You have to change following code

String stringOperand = "(([a-zA-Z_]*)\\.([a-zA-Z_]*))"; 

TO

String stringOperand = "(([a-zA-Z_0-9]*)\\.([a-zA-Z_0-9]*))"; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM