开发者

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开发者_如何学JAVA 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);

        }


The names in the example you've given appear to contain numbers, which your regex doesn't match.


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]*))"; 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜