将正则表达式代码从php转换为java无效

将正则表达式代码从php转换为java无效

问题描述:

I'm trying to convert this code from PHP to Java and I cannot make it work identically:

PHP:

function check_syntax($str) {

    // define the grammar
    $number = "\d+(\.\d+)?";
    $ident  = "[a-z]\w*";
    $atom   = "[+-]?($number|$ident)";
    $op     = "[+*/-]";
    $sexpr  = "$atom($op$atom)*"; // simple expression

    // step1. remove whitespace
    $str = preg_replace('~\s+~', '', $str);

    // step2. repeatedly replace parenthetic expressions with 'x'
    $par = "~\($sexpr\)~";
    while(preg_match($par, $str))
        $str = preg_replace($par, 'x', $str);

    // step3. no more parens, the string must be simple expression
    return preg_match("~^$sexpr$~", $str);
}

Java:

private boolean validateExpressionSintax(String exp){

    String number="\\d+(\\.\\d+)?";
    String ident="[a-z]\\w*";
    String atom="[+-]?("+number+"|"+ident+")";
    String op="[+*/-]";
    String sexpr=atom+"("+op+""+atom+")*"; //simple expression

    // step1. remove whitespace
    String str=exp.replaceAll("\\s+", "");

    // step2. repeatedly replace parenthetic expressions with 'x'
    String par = "\\("+sexpr+"\\)";

    while(str.matches(par)){
        str =str.replace(par,"x");
    }

    // step3. no more parens, the string must be simple expression
    return str.matches("^"+sexpr+"$");
}

What am I doing wrong? I'm using the expression teste1*(teste2+teste3) And i'm getting a match in php code but not in the java one, the line while(str.matches(par)) fails at first try. I assume this must be some problem with the matches method?

我正在尝试将此代码从PHP转换为Java,但我无法使其工作方式相同: p>

PHP: strong> p>

  function check_syntax($ str){
 
 //定义语法
 $ number  =“\ d +(\。\ d +)?”; 
 $ ident =“[az] \ w *”; 
 $ atom =“[+  - ]?($ number | $ ident)”; 
 $  op =“[+ * /  - ]”; 
 $ sexpr =“$ atom($ op $ atom)*”;  //简单表达式
 
 // step1。 删除空格
 $ str = preg_replace('〜\ s +〜','',$ str); 
 
 // step2。 用'x'
 $ par =“〜\($ sexpr \)〜”; 
 while(preg_match($ par,$ str))
 $ str = preg_replace($ par,'x'重复替换括号表达式 ,$ str); 
 
 // step3。 没有更多的parens,字符串必须是简单的表达式
 return preg_match(“〜^ $ sexpr $〜”,$ str); 
} 
  code>  pre> 
 
 

Java: strong> p>

  private boolean validateExpressionSintax(String exp){
 
 String number =“\\ d +(\\。\\ d +)?”  ; 
 String ident =“[az] \\ w *”; 
 String atom =“[+  - ]?(”+ number +“|”+ ident +“)”; 
 String op =“[+ * /  - ]“; 
 String sexpr = atom +”(“+ op +”“+ atom +”)*“;  //简单表达式
 
 // step1。 删除空格
 String str = exp.replaceAll(“\\ s +”,“”); 
 
 // step2。 用'x'重复替换括号表达式
 String par =“\\(”+ sexpr +“\\)”; 
 
 while(str.matches(par)){
 str = str.replace(par,  “x”); 
} 
 
 // step3。 没有更多的parens,字符串必须是简单的表达式
返回str.matches(“^”+ sexpr +“$”); 
} 
  code>  pre> 
 
 

我是什么 做错了? 我正在使用表达式 teste1 *(teste2 + teste3) code>我在PHP代码中得到一个匹配但不在java代码中,行 while(str.matches(par)) ) code>首先尝试失败。 我认为这必须是匹配方法的一些问题? p> div>

String.matches in Java will check that the whole string matches against the regex (as if the regex has ^ at the beginning and $ at the end).

You need Matcher to find some text inside a string that matches some regex:

Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(inputString);

while (matcher.find()) {
    // Extract information from each match
}

In your case, since you are doing replacement:

Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(inputString);

StringBuffer replacedString = new StringBuffer();

while (matcher.find()) {
    matcher.appendReplacement(replacedString, "x");
}

matcher.appendTail(replacedString);